質問失礼いたします
プログラミングの経験がなく、VBA自体を初めて触るものです
VBAを使って口座情報のtxtファイルを管理するプログラムの改修作業ですが、excelワークシートに入力する場所を30枠用意し、間に空白のセルがあってもちゃんと検索し、出力できるようにしたいです
わからないなりに書き換えてみましたが、Public Sub executeSeachAndOutput(ByVal inputDataList As Variant)
の部分で処理が止まってしまいます
どのようにすればいいか、どこを改修すればいいのかヒントでもいいので教えていただきたいです
また、こうした処理をする場合、テキストボックスを使用するのとセルに入力してもらうのとどちらの形の方が作りやすいですか?
VBA
1'標準モジュール 2 3'ファイル取得処理 4Public Sub GET_TextFile() 5 Dim objFS As Object 6 Dim strPath As String 7 Dim strFile As String 8 Dim strFolder As String 9 Dim ofdFolderDlg As Office.FileDialog 10 11 strPath = Range("selectFileName").Value 12 Set objFS = CreateObject("Scripting.FileSystemObject") 13 14 ' 初期パスの設定 15 If Len(strPath) > 0 Then 16 ' 末尾の"\"削除 17 If Right(strPath, 1) = "\" Then 18 strPath = Left(strPath, Len(strPath) - 1) 19 End If 20 21 ' ファイルが存在 22 If objFS.FileExists(strPath) Then 23 ' ファイル名のみ取得 24 strFile = objFS.GetFileName(strPath) 25 ' フォルダパスのみ取得 26 strFolder = objFS.GetParentFolderName(strPath) 27 ' ファイルが存在しない 28 Else 29 ' フォルダが存在 30 If objFS.FolderExists(strPath) Then 31 strFile = "" 32 strFolder = strPath 33 ' フォルダが存在しない 34 Else 35 ' ファイル名のみ取得 36 strFile = objFS.GetFileName(strPath) 37 ' 親フォルダを取得 38 strFolder = objFS.GetParentFolderName(strPath) 39 ' 親フォルダが存在しない 40 If Not objFS.FolderExists(strFolder) Then 41 strFolder = ThisWorkbook.Path 42 End If 43 End If 44 End If 45 Set objFS = Nothing 46 Else 47 strFolder = ThisWorkbook.Path 48 strFile = "" 49 End If 50 51 ' ファイル選択ダイアログ設定 52 Set ofdFileDlg = Application.FileDialog(msoFileDialogFilePicker) 53 With ofdFileDlg 54 .ButtonName = "選択" 55 '「ファイルの種類」をクリア 56 .Filters.Clear 57 '「ファイルの種類」を登録 58 .Filters.Add "テキストファイル", "*.txt", 1 59 .Filters.Add "全ファイル", "*.*", 2 60 61 ' 初期フォルダ 62 .InitialFileName = strFolder & "\" & strFile 63 ' 複数選択不可 64 .AllowMultiSelect = False 65 '表示するアイコンの大きさを指定 66 .InitialView = msoFileDialogViewDetails 67 End With 68 69 70 71 ' フォルダ選択ダイアログ表示 72 If ofdFileDlg.Show() = -1 Then 73 ' フォルダパス設定 74 strPath = ofdFileDlg.SelectedItems(1) 75 Else 76 ' キャンセルされた場合以降の処理は行なわない 77 Exit Sub 78 End If 79 80 Range("selectFileName").Value = strPath 81 Dim all As New Collection 82 Set all = New Collection 83 Set all = READ_TextFile(strPath) 84 85 '検索出力実行 86 Dim main As New suzukiMain 87 Call main.executeSeachAndOutput(all) 88 Set ofdFileDlg = Nothing 89 90 MsgBox "CSVファイルを" & Chr(13) & strPath & Chr(13) & "に出力完了しました。" 91 92End Sub 93' ファイルの読み込み処理 94'配列に格納する処理 95Private Function READ_TextFile(ByVal strPathName As String) As Collection 96 Dim intNo As Integer 97 Dim objFS As Object 98 Dim strBuff As String 99 strPath = strPathName 100 101 Set objFS = CreateObject("Scripting.FileSystemObject") 102 103 If objFS.FileExists(strPath) = False Then 104 Exit Function 105 End If 106 107 ' ファイルオープン 108 intNo = FreeFile() ' フリーファイルNoを取得 109 Open strPathName For Input As #intNo ' ファイルをオープン 110 111 ' ファイルの読み込み 112 Dim arrayList As New Collection 113 Set arrayList = New Collection 114 Dim readList As New Collection 115 Set readList = New Collection 116 117 Do Until EOF(intNo) ' ファイルの最後までループ 118 119 Line Input #intNo, strBuff ' ファイルから一行読み込み 120 121 122 If Left(strBuff, 1) <> 2 Then '区分コードが2以外の場合次の行へ 123 GoTo nextLine 124 End If 125 126 readList.Add Trim(Mid(strBuff, 51, 30)) '氏名 127 readList.Add Trim(Mid(strBuff, 6, 15)) '銀行名 128 readList.Add Trim(Mid(strBuff, 24, 19)) '支店名 129 readList.Add Trim(Mid(strBuff, 2, 4)) '銀行コード 130 readList.Add Trim(Mid(strBuff, 21, 3)) '支店コード 131 readList.Add Trim(Mid(strBuff, 43, 8)) '口座番号 132 readList.Add Trim(Mid(strBuff, 43, 1)) '口座種類 133 arrayList.Add readList '読み込んだ値をリストに格納 134 Set readList = New Collection 'リスト初期化 135 136nextLine: 137 138 Loop 139 140 ' ファイルクローズ 141 Close #intNo 142 143 '戻り値設定 144 Set READ_TextFile = arrayList 145 146End Function 147 148 149 150 151 152 153'ここからクラスモジュール 154 155 156 157 158'クラスモジュールcommon 159Public Function getMaxRow(ByVal sheetName As String, ByVal cal As Long) As Long 160'最大行取得 161Dim maxRow As Long 162 163'下から 164maxRow = ThisWorkbook.Sheets(sheetName).Cells(Rows.count, cal).End(xlUp).row 165 166'結果を返却 167getMaxRow = maxRow 168 169End Function 170 171Public Function getDataList(ByVal sheetName As String, ByVal startRow As Long, ByVal cal As Long) As Collection 172'データリスト取得 173Dim resultList As Collection 174Set resultList = New Collection 175 176'最大行取得 177Dim maxRow As Long 178maxRow = getMaxRow(sheetName, cal) 179 180'最大行まで取得 181Dim takeData As String 182Dim count As Long 183For count = startRow To maxRow 184 takeData = ThisWorkbook.Sheets(sheetName).Cells(count, cal).Value 185 resultList.Add takeData 186Next count 187 188'結果を返却 189Set getDataList = resultList 190 191End Function 192 193 194 195 196 197 198 199'クラスモジュールsearchName 200 201Public Function executeSeach(ByVal inputDataList As Variant, ByVal nameList As Variant) As Variant 202 203Dim hitDataList(29) As Variant 204 205Dim inputItemList(29) As Variant 206 207Dim inputNameData(29) As Variant 208Dim nameItem(29) As Variant 209Dim nameData(29) As Variant 210 211 212For Each inputItemList In Range("B12:B41") 213 nameData = inputItemList.Item 214 215 216For Each nameItem In nameList 217 If InStr(nameData, nameItem) <> 0 Then 218 hitDataList.Add inputItemList 219 End If 220 Next nameItem 221 222End Function 223
以上について回答宜しくお願いします
< 使用 Excel:Excel2013、使用 OS:Windows7 >
回答3件
あなたの回答
tips
プレビュー