导出Excel选项卡以分隔PDF文件 - 跳过前3个选项卡

时间:2018-05-16 13:55:13

标签: excel vba excel-vba pdf

Excel中的VBA编码新手,并试图找出如何修改我当前的代码(见下文)。它为我的每个选项卡创建了一个PDF,但我只是想让它忽略我工作的前3个选项卡,只创建从第4个选项卡开始的PDF。我确定定义范围是一个简单的补充,但无法准确指出如何编写它。

Sub ExportToPDFs()
' PDF Export Macro
' Change C:\Exports\ to your folder path where you need the files saved
' Save Each Worksheet to a separate PDF file.

Dim ws As Worksheet

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

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

Next ws

End Sub

感谢你们所能给予的任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

假设您要跳过的工作表是索引1,2和3,您可以使用For循环跳过这些工作表,并从工作表4开始:

Sub exportToPDF()
Dim ws As Worksheet
Dim i As Long
Dim nm As String

For i = 4 To ThisWorkbook.Worksheets.Count
    With ThisWorkbook.Worksheets(i)
        nm = .Name
        .ExportAsFixedFormat Type:=xlTypePDF, _
                                    Filename:="File Location" & nm & ".pdf", _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
Next i
End Sub