Saving as .xlsx in different directory in vba

16,398

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.

Share:
16,398
mike
Author by

mike

Updated on June 14, 2022

Comments

  • mike
    mike almost 2 years

    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?

  • Siddharth Rout
    Siddharth Rout about 12 years
    reverse the slashes and try again :) "C:\Users\mikefree\Desktop\test.xlsx"
  • mike
    mike about 12 years
    Fair enough, thanks. Sorry for that, I've used forward slashes before without trouble.