0

I have a excel workbook that i want to save as a csv file. This is the code i use:

Application.DisplayAlerts = True

ActiveWorkbook.SaveAs FileName:= _
"C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\1. QUOTES\3. PROGRAM FILES\DOOR QT REQUESTS\DOORS_COMBINED.csv", FileFormat:=xlCSV _
, CreateBackup:=False


Workbooks.Open ("C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\1. QUOTES\3. PROGRAM FILES\DOOR QT REQUESTS\DOOR PROGRAM.xlsm")

Workbooks("DOORS_COMBINED.csv").Close

The problem is when i save it it does not look the same as the excel workbook.

Excel file:

  A        B      C       D
Item 1  Item 2  Item3   Item 4
Item 1  Item 2  Item3   Item 4
Item 1  Item 2  Item3   Item 4

.CSV File: (Everything gets combined into column A)

          A                    B        C       D
Item 1Item 2Item3Item 4
Item 1Item 2Item3Item 4
Item 1Item 2Item3Item 4

If i save this manually as a .csv it turns out correct.

I think this could be the problem. I set excel to read the delimeter as ; because my descriptions contain commas.

File opened in notebook

enter image description here

Notepad view when using VBA to SaveAs:

Notepad view when SaveAs manually:

14
  • Does Excel file actually contain commas between columns as you show? Also , you are saving the ActiveWorkbook as csv, then continue on with additional programming. Lines after are done in a csv file. When you exit, macros are lost. You might have conflict. Try saving to csv and exiting right after. Also might consider saving ActiveSheet. Commented Nov 14, 2015 at 20:38
  • I'm not sure I follow. Your code shows you saving DOORS_COMBINED.csv and then opening DOOR PROGRAM.xlsm. So when you're opening the file you're unhappy with, are you opening it manually? Does it prompt you to specify how the file is delimited by chance? Commented Nov 14, 2015 at 20:54
  • The excel file doesn't actually contain commas. I tried running the code i have by commenting out the open and leaving the close workbook in and it still does whats above. I tried commenting both the open and close, and this made me close the new saved as file manually. This prompts me if i wanna save and everything and it then saves the file correctly. This is what I'm trying to get around. I want the user to just click the button and have it save correctly Commented Nov 14, 2015 at 20:59
  • @Marc I reopen PROGRAM.xlsm because my macros are in there and i need to run a few more operations after i save as. I tried a few things and was having conflicts with files being opened and closed. Was easier just to seperate the save as .csv. Maybe wont ave to do that after i figure this out. Yes I'm opening it the output file manually because I'm checking to see if it save right. I have another button that does stuff to it after this. When saving it does not ask what delimiter to use. Let me know if i can clarify anything else. Commented Nov 14, 2015 at 21:05
  • Excel does not recognize the delimiter? Commented Nov 14, 2015 at 21:34

2 Answers 2

1

I think the solution has been alluded to multiple times but to be clear.

This works for me:

ActiveWorkbook.SaveAs Filename:="C:\Temp\test_out.csv", FileFormat:=xlCSV, CreateBackup:=False, local:=True

The trick is the local:=True and making sure that your regional settings List separator is a semicolon.

To set the List separator, open Control Panel => Region and Language, click on Additional settings and change List separator: to ;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @codersl I dunno what i did before but i must have been in a hurry. Thanks for all the help! Works like a charm!
0

You can try this format

FileFormat:=xlCSVMSDOS

and try to add after CreateBackup:=False this line

, Local:=True

1 Comment

Just tried this and it doesnt work. It saves it as a blank .csv file. I had my finger crossed =) @Oskar Shon Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.