2

I have a problem with sheet hiding. I want to print the document, which includes the sheets with a similar name.

I did something like this:

  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

following the solutions:

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/

it returns over 2000 pages in my PDF document

enter image description here

What have I done wrong in the code?

I have about 30 sheets at most. How can I exclude (hide) the following sheets from printing it as the PDF?

1
  • Do you want the printsettings to be so? Paper format, margins etc? Commented Mar 24, 2021 at 14:00

3 Answers 3

2

I would suggest to do it with the print function. Here is my code:

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
Sign up to request clarification or add additional context in comments.

5 Comments

It says, that it's not enough memory to finish the print. It still tends to print over 2000 pages.
Then try to put "For i = 0 To UBound(arr) Debug.Print arr(i) Next" after the for loop which i added an look in the debug area it will show you all the sheets it tries to print.
It doesn't work either: If Worksheets(i).Visible = True Then subscript out of range
But does it show you the workbooks? Or does it stop a the if Worksheets(i).visible=true line?
I found an error, which wasn't related to the situation. Thanks for help. Your code is good.
1

You just need to select multiple sheets and make only the selected sheets into a pdf file.

Sub DPPtoPDF()

    Dim ws As Worksheet
    Dim vName() As Variant
    Dim n As Integer

    ReDim vName(1 To 1000)
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name Like "OTDR*" Or ws.Name = "Readme" Or ws.Name = "Asbuilt Photos 1" _
          Or ws.Name = "Asbuilt Photos 2" Or ws.Name = "Splicing Photos" _
          Or ws.Name = "Sign Off Sheet" Then
        Else
          n = n + 1
          vName(n) = ws.Name
        End If
    Next ws
    ReDim Preserve vName(1 To n)
    Sheets(vName).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        ThisWorkbook.Path & "\" & "test.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub

4 Comments

Still the same, I am getting 2084 pages exactly. The same situation as with my code and answers above.
@MKR, "OTDR*" to " ~*OTDR~* " . Does the sheet name begin with OTDR or is it included in the sheet name?
It starts from OTDR
@MKR, Can you post a screenshot of the sheet tab so that the sheet name is visible?
0

Print Specified Worksheets

Option Explicit

Sub DPPtoPDF()

    Const SheetNamesList As String = "Readme,Asbuilt Photos 1," _
        & "Asbuilt Photos 2,Splicing Photos,Sign Off Sheet"
    Const crit As String = "OTDR*"
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim SheetNames() As String: SheetNames = Split(SheetNamesList, ",")
    
    Dim sh As Object
    Dim snms() As String
    Dim shName As String
    Dim n As Long
    
    For Each sh In wb.Sheets
        shName = sh.Name
        If Not UCase(shName) Like crit Then
            If IsError(Application.Match(shName, SheetNames, 0)) Then
                 ReDim Preserve snms(n)
                 snms(n) = shName
                 n = n + 1
            End If
        End If
    Next sh
    
    ' Copy to new workbook
    wb.Worksheets(snms).Copy
    
    With ActiveWorkbook
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=wb.Path & "\" & wb.Name, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
        .Close False
    End With
    
    wb.Sheets("Frontsheet").Select

End Sub

7 Comments

Instead of PDF I am receiving a new Excel book including the sheets considered.
Still working on my end. If the workbook name is test.xlsm it will create the file test.xlsm.pdf (Office 2019). Maybe you should use Left(wb.Name, InstrRev(wb.Name, ".") - 1) instead of wb.Name to get test.pdf.
Nope, still the same mate.
Here's a link to my file on Google Drive. You have to download it (down arrow on the top right).
See in the lik what Iam getting, also from your file: imgur.com/a/1cO6w4H Maybe in my Excel settings is something wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.