打印工作簿中的所有工作表(3张特定工作表除外

时间:2015-07-09 08:42:14

标签: excel vba printing

我想打印我正在工作的工作簿中的所有工作表,除了三个特定的工作表,其中包括" Front Page"," Data"和" Logs"。我已尝试使用"和" - 以及"或" -statement和包裹的括号,没有任何效果。

以下是代码:

Sub printsheets()
Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        If  ws.Name <> "Front Page" And _
            ws.Name <> "Data" And _
            ws.Name <> "Logs" Then
            ws.PrintOut
        End If
    Next ws

End Sub

2 个答案:

答案 0 :(得分:1)

您的工作簿中似乎有隐藏的工作表。

在打印之前,您需要取消隐藏它们,或检查工作表是否隐藏。手动或(特别是如果您的工作表以编程方式隐藏xlSheetVeryHidden):

ws.Visible = xlSheetVisible ' ADD THIS LINE TO UNHIDE A SHEET
ws.PrintOut

If ws.Visible = xlSheetVisible Then
   ws.PrintOut
End if

如果您不想打印隐藏的工作表,请尝试使用这样的内容:

Sub printsheets()
 Dim ws As Worksheet
 For Each ws In ActiveWorkbook.Worksheets
  If ws.Visible = True Then
   If ws.Name <> "Front Page" And _
    ws.Name <> "Data" And _
    ws.Name <> "Logs" Then
       With ws.PageSetup
        .PrintArea = "b2:g26" ' USE YOUR PRINTAREA
        .Zoom = False
        .FitToPagesTall = 1
        .FitToPagesWide = 1
       End With
       ws.PrintOut
   End If
  End If
 Next ws
End Sub

答案 1 :(得分:0)

您还可以将一个列表与不想打印的表格一起使用

Sub printsheets()
    Dim dontPrint As Object
    Dim ws As Worksheet
    Set dontPrint = CreateObject("Scripting.Dictionary")

    dontPrint.Add "Front Page", 1
    dontPrint.Add "Data", 2
    dontPrint.Add "Logs", 3


    For Each ws In ActiveWorkbook.Worksheets
     If dontPrint.Exists(ws.Name) Then
     Else
        ws.PrintOut
     End If
    Next ws

End Sub