Macro to Export Certain Excel Sheets to Separate PDF

我们两清 提交于 2020-01-23 14:14:36

问题


I have a macro to export certain sheets in a workbook to separate PDF's (for reporting purposes). It works properly for me in one workbook, however, in a different workbook it is exporting ALL sheets. I can't figure out where I am going wrong. To make things easier, I italicized the spots where I would customize it for my purposes.

Sub ExportToPDFs()  
' PDF Export Macro  
' C:\ *location*  
' Sheets(Array("*selected sheets*")).Select  

Dim nm As String  
Dim ws As Worksheet  

For Each ws In Worksheets  
ws.Select  
nm = ws.Name  

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _  
Filename:="C:\*location*" & "*Report Title*" & nm & Range("D8").Value & ".pdf", _  
Quality:=xlQualityStandard, IncludeDocProperties:=True, _  
IgnorePrintAreas:=False, OpenAfterPublish:=True  

Next ws  

End Sub

回答1:


You need to fill the array sheets_to_select with the names of the sheets you want this to export. Otherwise, this will work for you.

Sub ExportToPDFs()  
' PDF Export Macro  
' C:\ *location*  
' Sheets(Array("*selected sheets*")).Select  

Dim nm As String  
Dim ws As Worksheet  
Dim i as Variant, sheets_to_select as Variant

sheets_to_select = Array("Sheet1","Sheet2","Sheet3")

For Each i in sheets_to_select  
   Thisworkbook.Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, _  
      Filename:="C:\*location*" & "*Report Title*" & i & Thisworkbook.Sheets("*The sheet you need the value of D8 from").Range("D8").Value & ".pdf", _  
      Quality:=xlQualityStandard, IncludeDocProperties:=True, _  
      IgnorePrintAreas:=False, OpenAfterPublish:=True  

Next i

End Sub



回答2:


Great question! I think this should do what you want...jsut modify the script to suit your needs...

Sub ExportToPDFs()

Dim ws As Worksheet

For Each ws In Worksheets
ws.Select
nm = ws.Name

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\your_path_here\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Next ws

End Sub


来源:https://stackoverflow.com/questions/43236526/macro-to-export-certain-excel-sheets-to-separate-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!