質問編集履歴

2

.

2022/02/11 15:06

投稿

mai_
mai_

スコア0

test CHANGED
File without changes
test CHANGED
@@ -23,7 +23,7 @@
23
23
  ```VBA
24
24
  Public Type hanyou
25
25
  order_zip_code() As Variant
26
- order_address() As Variant
26
+ order_address1() As Variant
27
27
  order_name() As Variant
28
28
  order_kana() As Variant
29
29
  order_tel() As Variant

1

失礼致しました。

2022/02/11 15:05

投稿

mai_
mai_

スコア0

test CHANGED
File without changes
test CHANGED
@@ -21,13 +21,13 @@
21
21
  ### 該当のソースコード
22
22
 
23
23
  ```VBA
24
- Public Type XXX
24
+ Public Type hanyou
25
- zip_code() As Variant
25
+ order_zip_code() As Variant
26
- address() As Variant
26
+ order_address() As Variant
27
- name() As Variant
27
+ order_name() As Variant
28
- kana() As Variant
28
+ order_kana() As Variant
29
- tel() As Variant
29
+ order_tel() As Variant
30
- mail() As Variant
30
+ order_mail() As Variant
31
31
  End Type
32
32
 
33
33
  Function test(ary)
@@ -38,16 +38,123 @@
38
38
 
39
39
  For i = 1 To UBound(ary)
40
40
 
41
- hanyou(i).zip_code = ary(i, 21)
41
+ hanyou(i).order_zip_code = ary(i, 21)
42
- hanyou(i).address1 = ary(i, 22)
42
+ hanyou(i).order_address1 = ary(i, 22)
43
- hanyou(i).address2 = ary(i, 23)
43
+ hanyou(i).order_address2 = ary(i, 23)
44
- hanyou(i).name = ary(i, 24)
44
+ hanyou(i).order_name = ary(i, 24)
45
- hanyou(i).kana = ary(i, 25)
45
+ hanyou(i).order_kana = ary(i, 25)
46
46
 
47
47
  Next
48
48
 
49
49
  End Function
50
50
 
51
+
52
+
53
+
54
+ Sub Btn_Click()
55
+
56
+ Dim ar()
57
+ Dim arCSVdata() As Variant
58
+ Dim arHanyou() As Variant
59
+
60
+ FileType = "CSV ファイル (*.csv),*.csv"
61
+ prompt = "CSV File を選択してください"
62
+
63
+ '操作したいファイルのパスを取得
64
+ FilePath = SelectFile(FileType, prompt)
65
+ If FilePath = False Then
66
+ End
67
+ End If
68
+
69
+ Set FSO = CreateObject("Scripting.FileSystemObject")
70
+ With FSO.OpenTextFile(FilePath, 8)
71
+ ReDim ary(.Line - 1)
72
+ .Close
73
+ End With
74
+ Set FSO = Nothing
75
+
76
+
77
+ arCSVdata() = CsvToArray(FilePath, ar())
78
+
79
+ arHanyou() = test(arCSVdata())
80
+ End
81
+
82
+ Function SelectFile(FileType, prompt) As Variant
83
+ SelectFile = Application.GetOpenFilename(FileType, , prompt)
84
+ End Function
85
+
86
+ Function CsvToArray(a_sFilePath, a_sArLine())
87
+ Dim oFS As New FileSystemObject '// FileSystemObjectインスタンス
88
+ Dim oTS As TextStream '// TextStream変数
89
+ Dim sLine '// CSVファイルの行文字列
90
+ Dim iLineCount '// CSVファイル行数
91
+ Dim iCommaCount '// カンマ数
92
+ Dim v '// カンマ分割後の配列
93
+ Dim i '// ループカウンタ
94
+ Dim iColumn
95
+
96
+ '// 引数のファイルが存在しない場合は処理を終了する
97
+ If (oFS.FileExists(a_sFilePath) = False) Then
98
+ Exit Function
99
+ End If
100
+
101
+ '// 最終行を取得するために追加モードで開く
102
+ Set oTS = oFS.OpenTextFile(a_sFilePath, ForAppending)
103
+ '// CSVファイルの行数を取得
104
+ iLineCount = oTS.Line - 1
105
+ Call oTS.Close
106
+
107
+ '// TextStreamオブジェクト作成
108
+ Set oTS = oFS.OpenTextFile(a_sFilePath)
109
+
110
+ i = 0
111
+ iCommaCount = 0
112
+
113
+ '// ファイルループ
114
+ Do While oTS.AtEndOfStream <> True
115
+ '// 1行読み込み
116
+ sLine = oTS.ReadLine
117
+
118
+ '// カンマで文字列を分割し配列化
119
+ v = Split(sLine, ",")
120
+
121
+ '// 現在行をカンマで分割した数が今までより多い場合
122
+ If (iCommaCount < UBound(v)) Then
123
+ '// 最大カンマ数を更新
124
+ iCommaCount = UBound(v)
125
+ End If
126
+
127
+ i = i + 1
128
+ Loop
129
+ Call oTS.Close
130
+
131
+ Erase a_sArLine
132
+ ReDim a_sArLine(iLineCount - 1, iCommaCount)
133
+
134
+ '// TextStreamオブジェクト作成
135
+ Set oTS = oFS.OpenTextFile(a_sFilePath)
136
+
137
+ i = 0
138
+
139
+ '// ファイルループ
140
+ Do While oTS.AtEndOfStream <> True
141
+ '// 1行読み込み
142
+ sLine = oTS.ReadLine
143
+
144
+ '// カンマで文字列を分割し配列化
145
+ v = Split(sLine, ",")
146
+
147
+ '// カンマ区切り配列をループ
148
+ For iColumn = 0 To UBound(v)
149
+ '// カンマ区切りの内容を二次元配列に格納
150
+ a_sArLine(i, iColumn) = v(iColumn)
151
+ Next
152
+
153
+ i = i + 1
154
+ Loop
155
+ Call oTS.Close
156
+ CsvToArray = a_sArLine
157
+ End Function
51
158
  ```
52
159
 
53
160
  ### 試したこと