如何使用 vba 代码将日期添加到 excel 文件标题?

How can I add the date to the excel file title with vba code?

早上好,

目前我使用下面的代码从 sheet 5 个在 sheet 处具有相同名称的 Excel 文件创建。但是我想将当前日期添加为:“List AA 30.03.2022”。

 Sub EXCELS()

'Create excel files

 Dim i As Integer
 Dim name_file As String
 For i = 5 To Sheets.Count
 name_file = Sheets(i).Name

 Worksheets(i).Copy

 With ActiveWorkbook
.SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx", 
 FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
Next i


End Sub

我需要补充什么?

将行 .SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx", FileFormat:=xlOpenXMLWorkbook 替换为

.SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & Format(Date, " dd.mm.yyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook

编辑
根据您的评论,为了完全替换文件,您首先需要将旧文件名保存为变量,然后再将其删除。

因此,将整个 With 块替换为以下内容;

With ActiveWorkbook
    'variable to store the old file name:
    Dim OldFileName as String
    'assign the file's current name to the variable:
    OldFileName = .FullName
    'Now save the file with it's new name, then close it:
    .SaveAs _
        Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook
    .Close
End With
'Get rid of the leftover file with the old name:
Kill OldFileName 

试试这个:

ThisWorkbook.Path & "\" & name_file & " " & Format(Date, "DD.MM.YYYY") & ".xlsx"