0

我一直在使用 VBA 工作表打印多个工作表的代码,条件为 Visible=True 并排除特定工作表。我哪儿也去不了。

Sub Printetail()
'
' PrintDetail Macro
'
    Dim wsSheet As Worksheet
    If Not wsSheet Is Nothing Then

      If wsSheet.Visible = True 
         And wsSheet.CodeName <> "EstimateBasis" 
         And wsSheet.CodeName <> "CashFlow" 
         And wsSheet.CodeName <> "MaterialPVTable" 
         And wsSheet.CodeName <> "Material" 
         And wsSheet.CodeName <> "Instruction"
         And wsSheet.CodeName <> "DebugSheet"
         And wsSheet.CodeName <> "StateLocalTax" 
         And wsSheet.CodeName <> "Referene" 
      Then
          '???
      End If

      If wsSheet.CodeName = "ProjectInput" Then

          wsSheet.PageSetup.CenterFooter = ActiveSheet.Range("C6").Text & _
                                           "    -Estimate Date:" & _
                                           ActiveSheet.Range("F2").Text & _
                                           "    -Gate:" & _
                                           ActiveSheet.Range("F4").Text & _
                                           "    -Rev No." & _
                                           ActiveSheet.Range("F5").Text

          wsSheet.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False

      Else

          wsSheet.Visible = True
          wsSheet.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False

      End If

End If

End Sub
4

1 回答 1

1

wsSheetNothing因为你还没有分配它。您将需要迭代工作表ThisWorkbook;像这样的东西应该能让你到达某个地方:

Dim wsSheet As Worksheet
For Each wsSheet In ThisWorkbook.Worksheets

    If CanPrintThisSheet(wsSheet) Then PrintWorksheet wsSheet

Next

然后CanPrintThisSheet是一个Boolean-returning 函数,您可以在其中放置“可以打印一张纸”的所有条件,并且PrintWorksheet将是一个子/过程,您可以在其中放置页面设置和打印逻辑(这只不过是一个电话)ws.PrintOut

Function CanPrintThisSheet(sheet As Worksheet) As Boolean
    CanPrintThisSheet = sheet.Visible And Not StringMatchesAny(sheet.CodeName, _
             "EstimateBasis", _
             "CashFlow", _
             "MaterialPVTable", _
             "Material", _
             "Instruction", _
             "DebugSheet", _
             "StateLocalTax", _
             "Referene")
End Function

使用下面的“StringMatchesAny”之类的函数可以避免执行所有检查,因为一旦找到匹配项,该函数就会返回:

Function StringMatchesAny(string_source, ParamArray find_strings()) As Boolean

    'String-typed local copies of passed parameter values:
    Dim find As String, src As String, i As Integer, found As Boolean
    src = CStr(string_source)

    For i = LBound(find_strings) To UBound(find_strings)
        find = CStr(find_strings(i))
        found = (src = find)
        If found Then Exit For
    Next

    StringMatchesAny = found

End Function
于 2013-05-21T00:59:27.957 回答