需要修改此导出宏以进行文本制表符分隔输出

时间:2012-03-21 12:58:40

标签: excel vba excel-vba excel-2007

我在这里找到了一个脚本,它将文件中的每个工作表导出到.csv文件,但我需要调整它以将工作表导出为文本制表符分隔文件。我试图修改它,但它只是导出为没有分隔符的文本。这是原始代码:

Public Sub DoTheExport()
Dim FName As Variant
Dim Sep As String
Dim wsSheet As Worksheet
Dim nFileNum As Integer
Dim csvPath As String


Sep = InputBox("Enter a single delimiter character (e.g., comma or semi-colon)", _
"Export To Text File")
'csvPath = InputBox("Enter the full path to export CSV files to: ")

csvPath = GetFolderName("Choose the folder to export CSV files to:")
If csvPath = "" Then
    MsgBox ("You didn't choose an export directory. Nothing will be exported.")
    Exit Sub
End If

For Each wsSheet In Worksheets
wsSheet.Activate
nFileNum = FreeFile
Open csvPath & "\" & _
  wsSheet.Name & ".csv" For Output As #nFileNum
ExportToTextFile CStr(nFileNum), Sep, False
Close nFileNum
Next wsSheet

End Sub

以下是我修改它的方法:

Public Sub DoTheExport()
Dim FName As Variant
Dim Sep As String
Dim wsSheet As Worksheet
Dim nFileNum As Integer
Dim txtPath As String


'Sep = InputBox("Enter a single delimiter character (e.g., comma or semi-colon)", _
'"Export To Text File")
'csvPath = InputBox("Enter the full path to export CSV files to: ")

txtPath = GetFolderName("Choose the folder to export TXT files to:")
If txtPath = "" Then
    MsgBox ("You didn't choose an export directory. Nothing will be exported.")
    Exit Sub
End If

For Each wsSheet In Worksheets
wsSheet.Activate
nFileNum = FreeFile
Open txtPath & "\" & _
  wsSheet.Name & ".txt" For Output As #nFileNum
ExportToTextFile CStr(nFileNum), Sep, False
Close nFileNum
Next wsSheet

End Sub

提前致谢!

1 个答案:

答案 0 :(得分:2)

看起来好像你没有将Sep设置成任何东西,而它看起来好像应该是你的分隔符。在For Each..循环之前使用以下内容。

Sep = vbTab

或者

Sep = Chr(9)