vba 带有保存当前日期的宏

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

Macro with Save Current date

excelvbaexcel-vba

提问by Trying_hard

Is there a way to make a macro to save a file with the current day in the name. I want to save this off everyday with the correct date.

有没有办法制作一个宏来保存名称为当天的文件。我想每天用正确的日期保存它。

This is what I have as a macro, pretty simple, but I am having issues with getting the current date formula in the file name (if possible)

这就是我所拥有的宏,非常简单,但是我在获取文件名中的当前日期公式时遇到了问题(如果可能)

Sub Save()
    ActiveWorkbook.SaveAs Filename:="X:\file06-21-2012\.xlsm", FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub

So tomorrow i would want the marco to save it as file06-22-2012.

所以明天我希望马可将其保存为 file06-22-2012。

Thanks

谢谢

回答by Siddharth Rout

Like this?

像这样?

Sub Save()
    Dim FilePath As String
    Dim NewName As String

    FilePath = "X:\": NewName = FilePath & "file" & Format(Date, "MM-DD-YYYY") & ".xlsm"

    ActiveWorkbook.SaveAs Filename:=NewName, FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub

回答by Scott Holtzman

WIth all due respect to @HeadofCatering's answer, a simpler, more easily readable approach would be this, I think.

在充分尊重@HeadofCatering 的回答的情况下,我认为更简单、更易于阅读的方法是这样的。

Sub Save()

Dim dtDate As Date
dtDate = Date

Dim strFile As String
strFile = "X:\file" & Format(dtDate, "mm-dd-yyyy") & ".xlsm"

ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub