お世話になります。
以下のtab区切りテキストファイルをcsvに変換し、それらを結合するファイルですが、
開発環境によってエラーが提示されてしまいます。
まだ書きかけのコードで、端末A (Microsoft Excel 2013 15.0.4953.1000 MSO (15.0.4953.1000) 32bit)では実行が個別のcsvファイルの出力で止まり、端末B(Microsoft Excel for Office 365 MSO 16.0.11629.20238 32bit)ではdir関数のところで「実行時エラー13;型が一致しません」を提示します。
最終的な実行環境は端末Bです。
発生している原因が何か、どなたかお教えいただけますでしょうか。よろしくお願いいたします。
VBA
1 Sub table_to_merged_csv() 2 3Dim table_name As String 4Dim myfile As Variant 5Dim target As Variant 6Dim name As String, wpath As String 7 8 myfile = Application.GetOpenFilename( _ 9 FileFilter:="table ファイル (*.Table),*.Table", _ 10 MultiSelect:=True) 11 12 13If IsArray(myfile) Then 14 15Application.ScreenUpdating = False 16 17For Each target In myfile 18 Workbooks.OpenText Filename:=target, DataType:=xlDelimited, Comma:=True 19 table_name = ActiveWorkbook.name 20 table_name = Left(table_name, Len(table_name) - 6) 21 table_name = table_name & ".csv" 22 ActiveSheet.Range("A1").CurrentRegion.EntireColumn.AutoFit 23 ActiveWorkbook.SaveAs Filename:=table_name, FileFormat:=xlCSV 24 Workbooks(table_name).Close True 25name = Dir(target) 26wpath = Replace(target, name, "") 27Next target 28 29 Debug.Print (wpath) 30 31wfile = Dir(wpath & "\") 32Debug.Print (wfile) 33flag = 0 34Do While wfile <> "" 35If InStr(wfile, ".csv") Then 36flag = flag + 1 37If flag = 1 Then 38FileCopy wpath & "\" & wfile, ThisWorkbook.Path & "output.csv" 39Open ThisWorkbook.Path & "output.csv" For Output As #1 40Close #1 41End If 42Open ThisWorkbook.Path & "output.csv" For Append As #1 43Open wpath & "\" & wfile For Input As #2 44Do Until EOF(2) 45Line Input #2, w_str 46Print #1, w_str 47Loop 48Close #2 49Close #1 50End If 51wfile = Dir() 52Loop 53 54Kill wpath & "*.csv" 55 56Application.ScreenUpdating = True 57 58End If 59 60End Sub
デバックして結果は?
前の質問において指摘させていただいた下記の文法誤りが修正されていないのが気になります。
ステップイン実行し、動作の違いをご自身で確認してみてください。
wfile = Dir(wpath & "\" & ”*.csv”)
’wfile = Dir(wpath & "\")
If InStr(wfile, ".csv") > 0 Then
'If InStr(wfile, ".csv") Then
FileCopy wpath & "\" & wfile, ThisWorkbook.Path & "\" & "output.csv"
'FileCopy wpath & "\" & wfile, ThisWorkbook.Path & "output.csv"
あなたの回答
tips
プレビュー