Print worksheets to single PDF based on a list of worksheet names

sp2222

New Member
Joined
Nov 22, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi All - I've been searching a long time and can't solve this one.

My Workbook has a Worksheet named "Summary" with a list of various country names in Column A starting at A11. It is a dynamic list, so the countries in this list can change.

My Workbook also has Worksheets for most of the countries. They are named by the country name. There are some countries that appear in the Summary that I do not have a Worksheet for.

I'm trying to create a macro that will save the "Summary" worksheet and all the Country worksheets toa single PDF. If I do not have a worksheet for country, I just want to skip it.

Thanks for any help. I've learned a lot from browsing but this one is proving too advanced for me!
 

Attachments

  • Screenshot 2023-11-22 060558.png
    Screenshot 2023-11-22 060558.png
    18.8 KB · Views: 4

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this macro:
VBA Code:
Public Sub Save_Sheets_As_PDF()

    Dim currentSheet As Worksheet
    Dim cell As Range
    Dim ws As Worksheet
    
    With ThisWorkbook.Worksheets("Summary")
        Set currentSheet = ActiveSheet
        .Select
        For Each cell In .Range("A11", .Cells(.Rows.Count, "A").End(xlUp))
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(cell.Value)
            On Error GoTo 0
            If Not ws Is Nothing Then ws.Select False
        Next
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\All Countries.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        currentSheet.Select
    End With
    
End Sub
 
Upvote 0
Hi @John_w I have one follow up question. Can you update the macro so it will add one more additional sheet to the PDF package? This sheet is called "Introduction" and I would like it to be the first page.

Thanks so much for your assistance.
 
Upvote 0
VBA Code:
Public Sub Save_Sheets_As_PDF()

    Dim currentSheet As Worksheet
    Dim cell As Range
    Dim ws As Worksheet
    
    ThisWorkbook.Worksheets("Introduction").Select
    With ThisWorkbook.Worksheets("Summary")
        Set currentSheet = ActiveSheet
        .Select False
        For Each cell In .Range("A11", .Cells(.Rows.Count, "A").End(xlUp))
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(cell.Value)
            On Error GoTo 0
            If Not ws Is Nothing Then ws.Select False
        Next
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\All Countries.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        currentSheet.Select
    End With
    
End Sub

Note - this and the previous macro create the PDF with sheets in the same order as the workbook, regardless of the order of the sheet names in A11:A<last>. The "Introduction" sheet must be the first sheet in the workbook if you want it to appear as the first page.
 
Upvote 0

Forum statistics

Threads
1,215,112
Messages
6,123,162
Members
449,099
Latest member
afishi0nado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top