简体   繁体   English

VBA 到带有文件名、特定文件夹和日期的 SaveAS

[英]VBA to SaveAS with Filename, Specific Folder, and Date

I am trying to save file in a specific folder, add the filename, and add todays date.我正在尝试将文件保存在特定文件夹中,添加文件名,并添加今天的日期。 My VBA is not working.我的 VBA 打不通。 Any suggestions?有什么建议么?

Sub SaveFile()

ActiveWorkbook.SaveAs ("G:\Product Support\Platinum\Agents Case Reports\Michael\Saved Client Reports\CAF\CAF Open Case Report.xlsx") & Date

End Sub

You can do this:你可以这样做:

Public Sub SaveFile()

    Dim formattedDate As String
    formattedDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
    
    Dim filename As String
    ' path + filename + formatted date + extension
    filename = _
      "G:\Product Support\Platinum\Agents Case Reports\Michael\Saved Client Reports\CAF\" & _
      "CAF Open Case Report - " & _
      formattedDate & _
      ".xlsx"
    
    ActiveWorkbook.SaveAs filename

End Sub

Whenever I output a date on a filename I always make sure it will sort chronologically.每当我 output 文件名上的日期时,我总是确保它会按时间顺序排序。 In the code above this is yyyy-mm-dd hh-mm-ss , which is year-month-day hour-minute-second.在上面的代码中,这是yyyy-mm-dd hh-mm-ss ,即年-月-日时-分-秒。 All numbers have leading zeroes, where necessary.必要时,所有数字都有前导零。 An example from a few moments ago is "2021-08-03 17-58-59".刚才的一个例子是“2021-08-03 17-58-59”。

File name cannot contain "/" character which is in the date you should firstly store Current Date in a variable then replace "/" from it before passing it to file name文件名不能包含日期中的“/”字符,您应该首先将当前日期存储在变量中,然后在将其传递给文件名之前从中替换“/”

Sub SaveFile()
Dim CurrentDate as string
CurrentDate = Date
CurrentDate = Replace(CurrentDate, "/", "_")
CurrentDate = Replace(CurrentDate, ".", "_")

ActiveWorkbook.SaveAs ("G:\Product Support\Platinum\Agents Case Reports\Michael\Saved Client Reports\CAF\CAF Open Case Report " & CurrentDate & ".xlsx") 
    
End Sub

This would work now.这现在可以工作了。

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

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