我在工作表隐藏方面遇到问题。我想打印该文档,其中包括具有相似名称的工作表。
我做了这样的事情:
Sub DPPtoPDF()
Dim ws As Worksheet
Sheets("Readme").Visible = False
Sheets("Asbuilt Photos 1").Visible = False
Sheets("Asbuilt Photos 2").Visible = False
Sheets("Splicing Photos").Visible = False
Sheets("Sign Off Sheet").Visible = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "OTDR*" Then
ws.Visible = False
End If
Next was
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Readme").Visible = True
Sheets("Asbuilt Photos 1").Visible = True
Sheets("Asbuilt Photos 2").Visible = True
Sheets("Splicing Photos").Visible = True
Sheets("Sign Off Sheet").Visible = True
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "OTDR*" Then
ws.Visible = True
End If
Next was
Sheets("Frontsheet").Select
End Sub
以下解决方案:
https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.visible
Unhide sheets names using VBA whose name contain specific word
https://excelchamps.com/vba/hide-sheet/
它返回我的 PDF 文档中的 2000 多个页面
我在代码中做错了什么?
我最多大约有30张。如何排除(隐藏)以下工作表以将其打印为 PDF?
最佳答案
我建议使用打印功能来完成此操作。这是我的代码:
Sub DPPtoPDF()
Dim ws As Worksheet
Sheets("Readme").Visible = False
Sheets("Asbuilt Photos 1").Visible = False
Sheets("Asbuilt Photos 2").Visible = False
Sheets("Splicing Photos").Visible = False
Sheets("Sign Off Sheet").Visible = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "OTDR*" Then
ws.Visible = False
End If
Next ws
Dim arr As Variant
Dim i As Integer
Dim counter As Integer
ReDim arr(counter)
'Add all visible sheets to an array (arr)
For i = 1 To Worksheets.Count
If Worksheets(i).Visible = True Then
ReDim Preserve arr(counter)
arr(counter) = Worksheets(i).Name
counter = counter + 1
End If
Next
'select all sheets the array contains
Worksheets(arr).Select
'set the path
printpath = ThisWorkbook.Path & "\" & ThisWorkbook.Name
'print the selection
ThisWorkbook.Windows(1).SelectedSheets.PrintOut PrintToFile:=True, PrToFileName:=printpath, ActivePrinter:="Microsoft Print to PDF", IgnorePrintAreas:=True
Sheets("Readme").Visible = True
Sheets("Asbuilt Photos 1").Visible = True
Sheets("Asbuilt Photos 2").Visible = True
Sheets("Splicing Photos").Visible = True
Sheets("Sign Off Sheet").Visible = True
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "OTDR*" Then
ws.Visible = True
End If
Next was
Sheets("Frontsheet").Select
End Sub
关于excel - VBA Excel在打印PDF时隐藏具有某些名称的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66780520/