Q&A
お世話になります。
定期的に発生するtab区切りテキストファイルをcsvファイルにし、
それを一つのcsvにまとめようとしています。
元のテキストファイルは半角英数字と"_"(アンダーバー)のみです。
後半の結合部分はこちらのサイトから拝借しました。
Filecopyで動作が終了しエラー53が返ってくるのですが、考えられる原因はなんでしょうか。
拝借したVBA個別であれば無論問題なく作動します。
wpath, wfile双方の内容は拝借分と変わらないことを確認しました。
また個別のcsvファイルを作成した後、5秒間動作を止めてから実行してみましたが、結果に変わりはありませんでした。
どうぞよろしくお願いいたします。
VBA
1Sub 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"v" 39Open ThisWorkbook.Path & "output.csv" For Output As #11 40Close #1 41End If 42Open ThisWorkbook.Path & "output.csv" For Append As #11 43Open wpath & "\" & wfile For Input As #22 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
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2019/06/23 12:53
2019/06/23 13:03 編集
2019/06/23 13:09
2019/06/23 13:13
2019/06/23 13:30
2019/06/23 13:34
2019/06/23 13:37
2019/06/23 13:40
2019/06/23 13:41
2019/06/23 13:45 編集
2019/06/23 13:49