EXCEL选项卡通过公式分隔出口

时间:2012-08-17 13:02:32

标签: excel vba csv excel-vba

我想从excel生成制表符分隔文件(使用公式设置字符串) 我不想使用另存为函数的原因是我将从一个电子表格生成许多不同的csv / txt文件,当你保存时,主文件的扩展名会被更改。

这就是我在Excel中所拥有的,该函数必须将所有列与中间的选项卡连接起来,Desc字段必须用双引号封装。

enter image description here

当我将单元格D2的内容复制到文本编辑器中时,返回以下字符串

"丰田Corrola"" desc here"""

正如您所见,Excel决定将双引号放入整个字符串并转义原始引号...

有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

为什么不将要放置的数据复制到.csv文件中,打开新的Excel实例,粘贴数据然后保存为.csv文件?

答案 1 :(得分:0)

虽然Excel制表符分隔导出工作完美,但我需要尽快在选区上生成制表符分隔文件。

找到/编辑下面的代码并为其分配一个Excel宏快捷方式:

    Sub QuoteCommaExport()
    Dim DestFile As String
    Dim FileNum As Integer
    Dim ColumnCount As Integer
    Dim RowCount As Integer

    ' Prompt user for destination file name.
    DestFile = InputBox("Enter the destination filename" & _
      Chr(10) & "(with complete path and extension):", _
      "Quote-Comma Exporter", "C:\myTabFile.txt")
    ' Obtain next free file handle number.
    FileNum = FreeFile()

    ' Turn error checking off.
    On Error Resume Next

    ' Attempt to open destination file for output.
    Open DestFile For Output As #FileNum
    ' If an error occurs report it and end.
    If Err <> 0 Then
      MsgBox "Cannot open filename " & DestFile
      End
    End If

    ' Turn error checking on.
    On Error GoTo 0

    Dim topString
    For ColumnCount = 1 To Selection.Columns.Count

        If ColumnCount < Selection.Columns.Count Then
            topString = topString & """" & ActiveSheet.Cells(ColumnCount, 1).Text & """" & vbTab
            Else
            topString = topString & """" & ActiveSheet.Cells(ColumnCount, 1).Text & """"
        End If
    Next ColumnCount
    Print #FileNum, topString

    ' Loop for each row in selection.
    For RowCount = 1 To Selection.Rows.Count
      ' Loop for each column in selection.
      For ColumnCount = 1 To Selection.Columns.Count

         ' Write current cell's text to file with quotation marks.
         Print #FileNum, """" & Selection.Cells(RowCount, _
            ColumnCount).Text & """";
         ' Check if cell is in last column.
         If ColumnCount = Selection.Columns.Count Then
            ' If so, then write a blank line.
            Print #FileNum,
         Else
            ' Otherwise, write a comma.
            ' Print #FileNum, ",";
              Print #FileNum, vbTab;
         End If
      ' Start next iteration of ColumnCount loop.
      Next ColumnCount
    ' Start next iteration of RowCount loop.
    Next RowCount

    ' Close destination file.
    Close #FileNum
End Sub