如何解决如何在导出宏名称中添加月份?
我有一个宏,可以使用工作表名称将工作表导出为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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。