Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

You can do Siddarth suggests and specify the directory by writing out the entire path. Alternatively if you wanted to have a user be able to change the directory the file is stored in when saving you could use:

ChDrive (filePath) ChDir (filePath)

Where filepath would be a starting directory for use with Application.GetSaveAsFilename. The user could then type in the name of the file or change directories if need be.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top