Saving as .xlsx in different directory in vba

mike picture mike · May 7, 2012 · Viewed 16.2k times · Source

I am trying to use the SaveAs method to save a .csv document as a .xls document. However, when I try and designate the folder, it fails.

For example, it will save to some default directory (my documents, which is not where I am ready from) here:

Sub csv()
Workbooks.Open Filename:="directory/tmp.csv"
ActiveWorkbook.SaveAs Filename:="test.xlxs", FileFormat:=51, CreateBackup:=False
End Sub

But this fails:

Sub csv()
Workbooks.Open Filename:="directory/tmp.csv"
ActiveWorkbook.SaveAs Filename:="otherdirectory/test.xlxs", FileFormat:=51, CreateBackup:=False
End Sub

Is there a different way to specify the directory?

Answer

Siddharth Rout picture Siddharth Rout · May 7, 2012

Use FileFormat:=xlCSV

This works for me.

ActiveWorkbook.SaveAs Filename:="C:\test.csv", FileFormat:=6, CreateBackup:=False

Whenever in doubt, record a macro :)

The FileFormat Constant is xlCSV. Either you can type FileFormat:=xlCSV or you can use it's actual value which is 6

EDIT

I see that you have edited the question :) Let me go through it again :)

Is there a different way to specify the directory? Thanks,

What is the exact path that you are specifying? I used Filename:="C:\Users\Siddharth Rout\Desktop\Book1.xlsx" and it works perfectly.