将排除添加到Excel宏

时间:2018-01-24 16:49:28

标签: excel excel-vba vba

所以我在运行时使用这个宏将各个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

我只想补充一下,不要从某个工作表开始或排除某些工作表。有什么线索?

1 个答案:

答案 0 :(得分: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