2017-05-28 99 views
0

因此,我有制表符分隔的CSV文件。 我有更改文件的内容宏,然后使用它的代码保存:打开制表符分隔的CSV并保存为制表符分隔

wb.Close SaveChanges:=True 

的问题是,那么它被保存为一个CSV逗号分隔。

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

我环顾四周,我没有找到一种方法将文件另存为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 
+0

TAB分隔文件是一个文本文件,您应该使用txt或tsv扩展名。 CSV是一个逗号分隔值文件,也是一个文本文件。 – Reeza

回答

1

因为该文件具有.csv而不是.txt扩展名。 The tab delimiter can be specified when opening

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 
+0

对不起。这没有用。 –

+0

@EstevaoSantiago什么都不起作用?我没有Excel,因此我无法在短时间内对其进行测试 – Slai

+0

保存的文件使用逗号而不是原始制表符输出。似乎wb.save完全忽略了加载格式,并用逗号分隔“强制”保存为CSV。 –