如何将月份添加到导出宏名称?

How to add month to an export macro name?

我有一个宏,可以使用 sheet 名称将作品sheet 导出为 pdf 到指定文件夹。我想要的是将月份添加为文件名的一部分。我可以从我的工作簿中的单元格中检索它。我应该在我的代码中的什么位置添加此引用?


Dim outFldr As String
Dim ws As Worksheet
Dim i As Variant, sheets_to_select As Variant

outFldr = ActiveWorkbook.Path
sheets_to_select = Array("Summary", "PLC", "MI", "Venture", "EIS", "VCT", "PE", "Debt")

For Each i In sheets_to_select
   ThisWorkbook.Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=outFldr & "\" & i & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False

Next i
MsgBox ("All pdf's exported.")
End Sub

从 Excel 中读取日期,例如使用 format 获取月份编号或名称并将其添加到文件名中。我更喜欢使用中间变量,因为这使代码更易于阅读和调试。

调整 format 以创建您想要的模式 - 您还可以包括年份。

For Each i In sheets_to_select
    Dim filename As String, filedate As Date, month As String
    filedate = ThisWorkbook.Sheets(1).Range("A1")       ' Change this so that it reads your cell with the date
    
    month = Format(filedate, "mm") ' For month as 2-digits
    month = Format(filedate, "mmm") ' For month as 3 letters month name
    month = Format(filedate, "mmmm") ' For month as full month name
    
    filename = outFldr & "\" & i & "_" & month & "_.pdf"
    
    ThisWorkbook.Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, _
        filename:=filename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
Next i