简体   繁体   中英

EXCEL Tab Delimited Export via Formula

I would like to generate a Tab Delimited file from excel (using a formula to setup the string) the reason that i dont want to use the save as function is that i will be generating many different csv/txt files from the one spreadsheet and when you save, the extension of the main file is changed.

this is what i have in Excel, the function must concatenate all columns with tabs in the middle, the Desc field must be encapsulated in double quotes.

在此处输入图片说明

when i copy the contents of cell D2 into a text editor returns the following string

"Toyota Corrola ""desc here"""

as you can see Excel decides to place double quotes into the entire string and escapes the original quotes...

Is there any way to fix this?

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

Although the Excel tab delimited export works perfectly, I needed a Tab Delimited file to be generated on a selection, as fast as possible.

Found/Edited the code below and assigned an Excel Macro Shortcut to it:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM