简体   繁体   English

具有保存当前日期的宏

[英]Macro with Save Current date

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. 所以明天我希望marco将其保存为file06-22-2012。

Thanks 谢谢

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM