将排除添加到Excel宏

问题描述 投票:0回答:1

所以我在运行时使用这个宏将各个excel工作表保存为自己的PDF:

Sub SaveWorksheetsAsPDFs()
Dim sFile       As String
Dim sPath       As String
Dim wks         As Worksheet

With ActiveWorkbook
    sPath = .Path & "\"
    For Each wks In .Worksheets
            sFile = wks.Name & ".pdf"
            wks.ExportAsFixedFormat Type:=xlTypePDF, _
                                    Filename:=sPath & sFile, _
                                    Quality:=xlQualityStandard, _
                                    IncludeDocProperties:=False, _
                                    IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=False
    Next wks
End With
End Sub

我想补充一点,不是从某个工作表开始或排除某些工作表。有什么线索怎么样?

excel excel-vba vba
1个回答
1
投票

您可以使用Select Case指定要忽略的工作表名称,如图所示。只需将“IgnoreSheet1”,“IgnoreSheet2”替换为您想要跳过的实际工作表名称。它只是一个以逗号分隔的列表,因此可以根据需要添加。

Sub SaveWorksheetsAsPDFs()
Dim sFile       As String
Dim sPath       As String
Dim wks         As Worksheet

With ActiveWorkbook
    sPath = .Path & "\"
    For Each wks In .Worksheets
        Select Case wks.Name
            Case "IgnoreSheet1", "IgnoreSheet2"     'Do nothing
            Case Else
                'Code here to run on the other sheets
                sFile = wks.Name & ".pdf"
                wks.ExportAsFixedFormat Type:=xlTypePDF, _
                                        Filename:=sPath & sFile, _
                                        Quality:=xlQualityStandard, _
                                        IncludeDocProperties:=False, _
                                        IgnorePrintAreas:=False, _
                                        OpenAfterPublish:=False
        End Select
    Next wks
End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.