vba Excel 另存为按钮宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6210617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:21:57  来源:igfitidea点击:

Excel Save As Button Macro

excelvbaexcel-vba

提问by Erik

I would like to create a VB "Save As" macro for Excel that would utilize the data from cell B7,B5and =NOWas the file name. This new file name would then be saved to a particular directory. (Ex. User clicks "Save" button. File name = (B5)ABCD_(B7)EFGH_=NOWis created and then saved to a directory of my choosing.

我想为 Excel 创建一个 VB“另存为”宏,它将利用来自 cell 的数据B7B5=NOW作为文件名。然后,这个新文件名将被保存到特定目录中。(例如,用户单击“保存”按钮。File name = (B5)ABCD_(B7)EFGH_=NOW创建然后保存到我选择的目录中。

I have found scripts that offer some of the singular options, but have had no luck finding or creating a script of my own with these options. Any help would be greatly appreciated.

我找到了提供一些单一选项的脚本,但没有运气找到或使用这些选项创建我自己的脚本。任何帮助将不胜感激。

回答by jonsca

You need to substitute for the invalid characters in the filename (they can't contain /or :) with periods or something else.

您需要用句点或其他东西替换文件名中的无效字符(它们不能包含/:)。

Sub DateFile()
    Dim str As String
    str = Range("B5").Value & "ABCD_" & Range("B7").Value & "EFGH" & Now()
    str = Replace(str, "/", ".")
    str = Replace(str, ":", ".")
    ActiveWorkbook.SaveAs (str)
End Sub

This can then be integrated into your push button code.

然后可以将其集成到您的按钮代码中。