vba - 将某些 Excel 工作表导出为单独的 PDF 的宏

标签 vba excel 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

最佳答案

您需要填写数组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

关于vba - 将某些 Excel 工作表导出为单独的 PDF 的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43236526/

相关文章:

excel - 更有效的写法是什么?

excel - VBA 自函数返回#VALUE!单元格出错,而在函数窗口中正确返回实际值

c++ - 使用系统函数打开 excel 表

ios - 在 ios9 上的 uiwebview 中显示 pdf 有时滚动会卡住

excel - 提高生成随机数的嵌套循环的速度

excel - 即使使用 xlCelltypevisible,Row.Count 也会计算不可见的单元格

excel - 使用 Excel VBA 生成 Code 128 条形码

arrays - 构建和比较数组

javascript - 将图像动态保存到 PDF 模板

javascript - 根据 ComboBox 选择设置字段