vba 在vba的不同目录中另存为.xlsx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10488895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving as .xlsx in different directory in vba
提问by mike
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.
我正在尝试使用该SaveAs
方法将 .csv 文档另存为 .xls 文档。但是,当我尝试指定文件夹时,它失败了。
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?
是否有不同的方式来指定目录?
回答by Siddharth Rout
Use FileFormat:=xlCSV
用 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
该FileFormat
常数是xlCSV
。您可以输入FileFormat:=xlCSV
或使用它的实际值,即 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.
您指定的确切路径是什么?我用过Filename:="C:\Users\Siddharth Rout\Desktop\Book1.xlsx"
,效果很好。
回答by Otherguy
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:
您可以通过写出整个路径来执行 Siddarth 建议并指定目录。或者,如果您希望用户能够在保存时更改文件的存储目录,您可以使用:
ChDrive (filePath) ChDir (filePath)
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.
其中 filepath 将是与 Application.GetSaveAsFilename 一起使用的起始目录。如果需要,用户然后可以键入文件的名称或更改目录。