VBA Excel 备份:通过 VBA 以新名称保存当前工作簿的副本,保留 VBA 脚本,无提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15810046/
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
VBA Excel Backup: Save copy of current workbook via VBA under a new name, keep VBA scripts, no prompt
提问by Martin Carlsson
I need to make a VBA script that can take a complete copy of the current workbook and save it under a new name.
我需要制作一个 VBA 脚本,该脚本可以获取当前工作簿的完整副本并将其保存在新名称下。
It need to be a complete copy with VBA scripts and everything.
它需要是包含 VBA 脚本和所有内容的完整副本。
The user may NOT be prompted for anything - the new file name (and location) will be provided by the code.
可能不会提示用户输入任何内容 - 新文件名(和位置)将由代码提供。
回答by Roger Rowland
Surely something like this is sufficient?
这样的东西肯定就足够了吗?
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Data.xlsm"
Application.DisplayAlerts = True
回答by Michal Jána
Try this:
尝试这个:
Activeworkbook.SaveCopyAs "C:\Data.xlsm"
回答by Mike - SMT
I have noticed that at least for Excel 2013 ActiveWorkbook.SaveAs Filename:="C:\Data.xlsm
may not work as on my end it is saying that excel cannot save as file type xlsm.
我注意到至少对于 Excel 2013ActiveWorkbook.SaveAs Filename:="C:\Data.xlsm
可能无法正常工作,因为我最后说 excel 无法保存为文件类型 xlsm。
However I did find a work around that is working for myself and wanted to add it here as a reference for others who may run into the same problem.
但是,我确实找到了一个对自己有用的解决方法,并希望将其添加到此处作为其他可能遇到相同问题的人的参考。
If you add , FileFormat:=52
after your code it will be able to save as the xlsm
format.
如果, FileFormat:=52
在代码之后添加,它将能够保存为xlsm
格式。
So this worked for me:
所以这对我有用:
ActiveWorkbook.SaveAs Filename:="C:\Data.xlsm, FileFormat:=52"
Happy coding!
快乐编码!