繁体   English   中英

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

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

我正在尝试将文件保存在特定文件夹中,添加文件名,并添加今天的日期。 我的 VBA 打不通。 有什么建议么?

Sub SaveFile()

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

End Sub

你可以这样做:

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

每当我 output 文件名上的日期时,我总是确保它会按时间顺序排序。 在上面的代码中,这是yyyy-mm-dd hh-mm-ss ,即年-月-日时-分-秒。 必要时,所有数字都有前导零。 刚才的一个例子是“2021-08-03 17-58-59”。

文件名不能包含日期中的“/”字符,您应该首先将当前日期存储在变量中,然后在将其传递给文件名之前从中替换“/”

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

这现在可以工作了。

暂无
暂无

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

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