Macro with Save Current date

2020-06-28 08:50发布

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.

Thanks

2条回答
【Aperson】
2楼-- · 2020-06-28 09:33

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

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
查看更多
成全新的幸福
3楼-- · 2020-06-28 09:36

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
查看更多
登录 后发表回答