2012-08-19 48 views
2

所以我有一個宏,它的工作原理如下。它通過數據驗證下拉菜單循環,併爲每個國家的下拉菜單保存pdf。但是,當我嘗試編輯宏以便文件名包含除國家/地區名稱(D14)之外的日期時遇到運行時錯誤1004文檔無法保存。我是很新的VBA所以我不知道如何解決這個...我真的很感激一些幫助運行時錯誤1004當試圖用文件名保存文件時沒有保存文件

斯蒂芬

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 
+2

D14的值是否包含帶「/」字符的日期?如果是這樣,文件名不能有特殊字符,例如:,/等。 – shahkalpesh 2012-08-19 15:39:15

回答

0

清潔你的特殊字符日期值。

假設範圍將永遠是一個日期,更換:

ActiveSheet.Range("D14").Value 

像這樣的東西:

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

隨意使用不同的格式"YYYYMMDD",但要確保你不如shahkalpesh的評論所示,使用「/」。