我一直在使用代码打印多个工作表使用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
答案 0 :(得分:1)
wsSheet
是Nothing
,因为您尚未分配任何内容。您需要迭代ThisWorkbook
的工作表;这样的事情会让你到处找个地方:
Dim wsSheet As Worksheet
For Each wsSheet In ThisWorkbook.Worksheets
If CanPrintThisSheet(wsSheet) Then PrintWorksheet wsSheet
Next
然后CanPrintThisSheet
将成为Boolean
- 返回函数,您可以将所有条件设置为“可以打印表格”,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