vba 保存电子表格时根据单元格数据自动命名文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5710757/
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
Automatically name a file based on cell data when saving a spreadsheet?
提问by Nathan L.
I have an .xltm template spreadsheet that I'm wondering if I can get a macro to populate the "save as" file name based on cell data, is this possible?
我有一个 .xltm 模板电子表格,我想知道是否可以获得一个宏来根据单元格数据填充“另存为”文件名,这可能吗?
There are over 50 people who will have this spreadsheet, it's more of a form, and we are trying to figure out a way to keep the filenames uniform. I know there is the ThisWorkbook.BeforeSave, but I'm not really having any luck there. I just need it to make a file named something like $A$1 R $B$1 T $B$3.xlsx
有超过 50 人将拥有这个电子表格,它更像是一种表格,我们正试图找出一种方法来保持文件名的统一。我知道有 ThisWorkbook.BeforeSave,但我真的没有运气。我只需要它来制作一个名为类似的文件$A$1 R $B$1 T $B$3.xlsx
Any ideas on how to do this?
关于如何做到这一点的任何想法?
回答by Jean-Fran?ois Corbett
Sure.
当然。
Sub SaveMyWorkbook()
Dim strPath As String
Dim strFolderPath as String
strFolderPath = "C:\"
strPath = strFolderPath & _
Sheet1.Range("A1").Value & "R" & _
Sheet1.Range("B1").Value & "T" & _
Sheet1.Range("B3").Value & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strPath
End Sub
EDIT: After you clarified your question in your comment below, I can now safely say that the answer is: No, what you are asking is not possible.
编辑:在您在下面的评论中澄清您的问题后,我现在可以有把握地说答案是:不,您问的是不可能的。
What ispossible is to put a big, fat command button on your sheet that says "Press me to save", and have that button call the above Sub. You can set a fixed folder, as in the example above, or have the user pick a folder using the FileDialog
object (or the GetSaveAsFilename
function, but then the user will be able to change the suggested filename, so less safe).
什么是可能的是把你的表一个又大又肥的命令按钮,上面写着“按我保存”,并有一个按钮调用上述子。您可以设置一个固定文件夹,如上例所示,或者让用户使用FileDialog
对象(或GetSaveAsFilename
函数,但随后用户将能够更改建议的文件名,因此不太安全)选择一个文件夹。