Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Save as tab delimited file

Tags:

excel

vba

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?

like image 851
Heresh Avatar asked Sep 29 '16 09:09

Heresh


People also ask

How do I change the delimiter in Excel VBA?

I found the following: Go to Start>Settings>Regional And Language Options. Click on the Customize button. Next to List Separator type in a semi-colon (;)

How do you do Save As in Excel VBA?

read more to Save As the file is the F12 key. In VBA, too, we can save the file as “Save As.”


2 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}"
like image 162
Heresh Avatar answered Oct 20 '22 06:10

Heresh


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.

like image 1
Aditya Pansare Avatar answered Oct 20 '22 04:10

Aditya Pansare