打开制表符分隔的CSV并保存为制表符分隔

所以,我有一个制表符分隔的CSV文件。 我有一个macros来改变文件的内容,然后使用代码保存它:

wb.Close SaveChanges:=True 

问题是,它然后保存为CSV逗号分隔。

当另一个例程打开文件时,它会变得混乱。

我环顾四周,我没有find一种方法将文件保存为Excel中的TAB分隔CSV。

有人能帮我吗? 以下是完整的代码:

  Sub routine() Dim wb As Workbook Dim Path As String 'Caminho Dim File As String 'Arquivo da pasta Dim Folder As FileDialog 'Pasta de origem Dim answer As Integer '-------------------------------------------------------------------------------' answer = MsgBox("This macro will ask you to select a folder and change all the files from that folder. This action is not reversible, so make a backup before proceeding.", vbYesNo + vbInformation, "Confirm Action") If answer = vbYes Then Set Folder = Application.FileDialog(msoFileDialogFolderPicker) With Folder .Title = "Select Folder with CSV Files" .AllowMultiSelect = False If .Show <> -1 Then GoTo NextCode Path = .SelectedItems(1) & "\" End With 'Caso o usuário cancele NextCode: Path = Path If Path = "" Then GoTo Resetar File = Dir(Path & "*.csv*") Do While File <> "" Set wb = Workbooks.Open(Filename:=Path & File) Columns("A:A").Select Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ Semicolon:=False, Comma:=False, Space:=False, Other:=False, TrailingMinusNumbers:= _ True Range("N:N, M:M, L:L, K:K, J:J, H:H, G:G, F:F, D:D, C:C ").EntireColumn.Delete Range("A2").EntireRow.Delete wb.sav wb.Close SaveChanges:=True DoEvents File = Dir Loop MsgBox "CSV Files From Folder Updated" Else GoTo Resetar Resetar: MsgBox "User Cancelled Action" End If End Sub 

因为该文件具有.csv而不是.txt扩展名。 可以在打开时指定制表符分隔符 :

 Set wb = Workbooks.Open(Filename:=Path & File, Format:=1) Range("C:D, H:F, J:N").EntireColumn.Delete Rows(2).EntireRow.Delete Application.DisplayAlerts = False wb.Sheets(1).SaveAs Filename:=Path & File, FileFormat:=xlTextWindows wb.Close SaveChanges:=False Application.DisplayAlerts = True