我有一个宏,可以使用工作表名称将工作表导出为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
答案 0 :(得分:0)
从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