运行时错误1004当试图用文件名中的date保存文件时文档未保存

所以我有一个macros,它的工作原理如下。 它通过数据validation下拉菜单循环,并为每个国家的下拉菜单保存pdf。 但是,当我尝试编辑macros,以便文件名包括date除了国家(D14)名称我遇到运行时错误1004文档无法保存。 我对VBA很新,所以我不知道如何解决这个问题。我真的非常感谢你的帮助

斯蒂芬

Sub Create_PDFs() ' ' Create_PDFS Macro ' ' Keyboard Shortcut: Ctrl+y ' Const sheetToExportName = "Graphs" Const sheetWithCountryList = "Master Sheet" Const CountryListAddress = "AQ6:AQ38" Const chosenCountryCell = "D14" Const sheetWithChosenCell = "Graphs" Dim CountryList As Range Dim anyCountry As Range Set CountryList = _ ThisWorkbook.Worksheets(sheetWithCountryList). _ Range(CountryListAddress) For Each anyCountry In CountryList ThisWorkbook.Worksheets(sheetWithChosenCell). _ Range(chosenCountryCell) = anyCountry ThisWorkbook.Worksheets(sheetToExportName).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ "N:\International Finance Division\RAT Advanced Economies - Chartpacks\Country Risks\Created PDFs\" & ActiveSheet.Range("D14").Value & " - Country Risk Indicators.pdf" _ , Quality:=xlQualityStandard, IncludeDocProperties:=False, _ IgnorePrintAreas:=False, OpenAfterPublish:=False Next Set CountryList = Nothing End Sub 

清理特殊字符的date值。

假设范围始终是date,则replace:

 ActiveSheet.Range("D14").Value 

像这样的东西:

 format(ActiveSheet.Range("D14").Value,"YYYYMMDD") 

请随意使用与"YYYYMMDD"不同的格式,但请确保您不要使用“/”,如shahkalpesh的评论所示。