【问题标题】:EXCEL Tab Delimited Export via Formula通过公式 EXCEL 制表符分隔导出
【发布时间】:2012-08-17 13:02:31
【问题描述】:

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

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

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

"Toyota Corrola""在这里描述"""

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

有没有办法解决这个问题?

【问题讨论】:

    标签: vba csv excel


    【解决方案1】:

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

    【讨论】:

    • 是的,我会这样做,但是我的工作方式意味着我需要重复这个过程几百次。一旦您“另存为”,Excel 就会更改其格式,并且在此过程中有 4 个弹出框。所以基本上,我需要更快地工作。
    【解决方案2】:

    虽然 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多