Home » excel » Excel VBA Save as tab delimited file

Excel VBA Save as tab delimited file

Posted by: admin March 9, 2020 Leave a comment

Questions:

I have some code that saves the active worksheet as a tab delimited text file, however the cell data that has a comma character in them gets exported with quotation marks, like

John Hopkins, Burgers
becomes
"John Hopkins, Burgers"

How can I work around this?

This is my code:

ActiveWorkbook.SaveAs ActiveWorkbook.path & "\" & filename,
FileFormat:=xlText, CreateBackup:=False

Adding this:
I just found out that if I save the file again it removes all “”.
Can I add an additional save to the code?

How to&Answers:

I solved it! Since the quotes disappear when you save it manually as a delimited file but not when you use

ActiveWorkbook.SaveAs ActiveWorkbook.path & "\" & filename,
FileFormat:=xlText, CreateBackup:=False

I just added another save after the above one, but this time using the sendkey strokes to mimick the manual method.

ActiveWorkbook.SaveAs fil, FileFormat:= _
xlText

SendKeys "^{F4}"
SendKeys "{ENTER}"
SendKeys "{ENTER}"

Answer:

According to FileFormat Property, you have the following formats to choose from:

xlCSV
xlCSVMac
xlCSVMSDOS
xlCSVWindows

Why don’t you choose one of the comma- separated value formats?

Try xlCSV.

Answer:

This is a variation Of some code I used to save a normal xlsb file or xls* file
could be used for many different files though.

Sub SaveAsTDV()

'
' Easy way to export to Tab Deliminated Values
' By DeLaguna
' v17.05.10.15.06
'

Dim BaseFolder As String
    Dim LitProg As String
    Dim Path As String
    Dim Extn As String
      BaseFolder = "YourBase Folder\"    
      FileName = "FileName" 'File name with no Extension.
      Extn = ".txt" 'If choosing somthing other than xlText you may change this.

 Application.DisplayAlerts = False 'Auto Yes to save

    ChDir BaseFolder
        ActiveWorkbook.SaveAs Filename:= BaseFolder & FileName & Extn, _
        FileFormat:=xlText, CreateBackup:=True  'xlText will save as Tab Deliminated text

Application.DisplayAlerts = True ' Reverts to Excel Default showing popup messages for input.

End Sub

Hopefully it helps someone let me know if I made any errors I had to redact sensitive information out of it.

Answer:

Change FileFormat xlText to xlTextPrinter

 ActiveWorkbook.SaveAs ActiveWorkbook.path & "\" & filename,
    FileFormat:=xlTextPrinter, CreateBackup:=False