宏导出某些Excel表格以分离PDF

时间:2017-04-05 16:15:46

标签: excel vba excel-vba pdf

我有一个宏可以导出工作簿中的某些工作表以分隔PDF(用于报告目的)。它在一个工作簿中适合我,但是,在不同的工作簿中,它导出所有工作表。我无法弄清楚我哪里出错了。为了使事情变得更容易,我将为我的目的定制它的位置斜体。

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

2 个答案:

答案 0 :(得分:1)

您需要使用要导出的工作表的名称填充数组sheets_to_select。否则,这对你有用。

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

答案 1 :(得分:0)

好问题!我认为这应该做你想要的... jsut修改脚本以满足你的需求......

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