1

我编写了以下代码来打印工作表的特定页面,具体取决于选择了哪些活动工作表。一些工作表有需要过滤的列表。当我自己运行 printSheets() 过程时,它打印得很好。当我在打印前的过程中调用它时,它会打印两次选择中的第一个工作簿。谁可以给我解释一下这个?

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Call printingSheets
    End Sub

    Sub printingSheets()
    Dim ws As Worksheet
    Dim arySheets
    Dim selSheets
    Dim iSheet As Long
    Dim i As Long
    Set selSheets = ThisWorkbook.Windows(1).selectedSheets
    i = 0

    ReDim arySheets(selSheets.Count)
    For Each ws In selSheets

        arySheets(iSheet) = ws.Name
        iSheet = iSheet + 1
        With ws
        If ws.Name = "12-32" Then
        Worksheets("12-32").Activate
        x = x + 1
                 ws.ListObjects("Table9").Range.AutoFilter Field:=1, Criteria1:= _
                 "<>"
                ws.PageSetup.PrintArea = "$A$1:$J$249"
                ws.PrintOut From:=1, to:=1, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.PageSetup.PrintArea = "$A$1:$K$249"
                ws.PrintOut From:=3, to:=3, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.ListObjects("Table9").Range.AutoFilter Field:=1
                Application.EnableEvents = True
                Cancel = True
        ElseIf ws.Name = "13-33" Then
        Worksheets("13-33").Activate
        x = x + 1
                 ws.ListObjects("Table8").Range.AutoFilter Field:=1, Criteria1:= _
                 "<>"
                ws.PageSetup.PrintArea = "$A$1:$J$249"
                ws.PrintOut From:=1, to:=1, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.PageSetup.PrintArea = "$A$1:$K$249"
                ws.PrintOut From:=3, to:=3, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.ListObjects("Table8").Range.AutoFilter Field:=1
                Application.EnableEvents = True
                Cancel = True

        ElseIf ws.Name = "Cap11-31" Then
        Worksheets("Cap11-31").Activate
        x = x + 1
                 ws.ListObjects("Table10").Range.AutoFilter Field:=1, Criteria1:= _
                 "<>"
                ws.PageSetup.PrintArea = "$A$1:$J$249"
                ws.PrintOut From:=1, to:=1, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.PageSetup.PrintArea = "$A$1:$K$249"
                ws.PrintOut From:=3, to:=3, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.ListObjects("Table10").Range.AutoFilter Field:=1
                Application.EnableEvents = True
                Cancel = True

         ElseIf ws.Name = "Cap10-30" Then
        Worksheets("Cap10-30").Activate
        x = x + 1
                 ws.ListObjects("Table11").Range.AutoFilter Field:=1, Criteria1:= _
                 "<>"
                ws.PageSetup.PrintArea = "$A$1:$J$249"
                ws.PrintOut From:=1, to:=1, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.PageSetup.PrintArea = "$A$1:$K$249"
                ws.PrintOut From:=3, to:=3, Copies:=1, Collate:=True, IgnorePrintAreas:=False
                ws.ListObjects("Table11").Range.AutoFilter Field:=1
                Application.EnableEvents = True
                Cancel = True

        Else
        ws.PrintOut From:=1, to:=1, Copies:=1, Collate:=True, IgnorePrintAreas:=False
        Application.EnableEvents = True
        Cancel = True
        End If
        End With

    Next ws
    End Sub
4

0 回答 0