VBA - Save sheets whose names are in table to single pdf

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this. The sheet names are in the first column of the first table on "Sheet1". Change the path string variable to suit.

VBA Code:
Public Sub Save_Table_Sheets_As_PDFs()

    Dim path As String
    Dim table As ListObject
    Dim r As Long
    Dim ws As Worksheet
   
    path = "C:\Temp\Excel\PDF\"
   
    Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects(1)
  
    With table
        For r = 1 To .DataBodyRange.Rows.Count
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(.DataBodyRange(r, 1).Value)
            On Error GoTo 0
            If Not ws Is Nothing Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & ws.Name & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            End If
        Next
    End With
   
End Sub
 
Last edited:
Upvote 0
Try this. The sheet names are in the first column of the first table on "Sheet1". Change the path string variable to suit.

VBA Code:
Public Sub Save_Table_Sheets_As_PDFs()

    Dim path As String
    Dim table As ListObject
    Dim r As Long
    Dim ws As Worksheet
   
    path = "C:\Temp\Excel\PDF\"
   
    Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects(1)

    Set ws = ThisWorkbook.Worksheets("John")
   
    With table
        For r = 1 To .DataBodyRange.Rows.Count
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(.DataBodyRange(r, 1).Value)
            On Error GoTo 0
            If Not ws Is Nothing Then
                ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & ws.Name & ".pdf", _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            End If
        Next
    End With
   
End Sub
Thanks John_w! It works. Is it possible to get all the pdfs into one directly in Excel? I want page numbering throughout the pdf.
/Olov
 
Upvote 0
Sorry, I overlooked the single PDF requirement.

VBA Code:
Public Sub Save_Table_Sheets_As_Single_PDF()

    Dim path As String
   
    path = "C:\Temp\"
   
    With ThisWorkbook.Worksheets("Sheet1").ListObjects(1)
        ThisWorkbook.Worksheets(Application.Transpose(.DataBodyRange.Columns(1))).Select  'sheet names in column 1
    End With
   
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & "All Table Sheets.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 'False

End Sub
Assumes all sheet names exist.
 
Upvote 0
Thanks for the help. Can you modify the code so that it works if not all sheets names exist?
 
Upvote 0
VBA Code:
Public Sub Save_Table_Sheets_As_Single_PDF()

    Dim path As String
    Dim table As ListObject
    Dim r As Long
    Dim ws As Worksheet
    Dim replaceSheet As Boolean
    
    path = "C:\Temp\"
    
    replaceSheet = True
    Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects(1)
    With table
        For r = 1 To .DataBodyRange.Rows.Count
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(.DataBodyRange(r, 1).Value)
            On Error GoTo 0
            If Not ws Is Nothing Then
                ws.Select replaceSheet
                replaceSheet = False
            End If
        Next
    End With
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & "Sheets.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 'False
    
    table.Parent.Select
    
End Sub
 
Upvote 0
Solution
VBA Code:
Public Sub Save_Table_Sheets_As_Single_PDF()

    Dim path As String
    Dim table As ListObject
    Dim r As Long
    Dim ws As Worksheet
    Dim replaceSheet As Boolean
   
    path = "C:\Temp\"
   
    replaceSheet = True
    Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects(1)
    With table
        For r = 1 To .DataBodyRange.Rows.Count
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(.DataBodyRange(r, 1).Value)
            On Error GoTo 0
            If Not ws Is Nothing Then
                ws.Select replaceSheet
                replaceSheet = False
            End If
        Next
    End With
   
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & "Sheets.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 'False
   
    table.Parent.Select
   
End Sub
Thanks John_w! It works. You have made my day easier!
/Olov
 
Upvote 0
To John_W:
Hello again.
Can you change the code and enter choices, save as pdf or print?
 
Upvote 0
Can you change the code and enter choices, save as pdf or print?
You could do this:

VBA Code:
    Dim response As Variant
    response = MsgBox("Click Yes to Save as PDF, or No to Print, or Cancel", vbYesNoCancel, "Save as PDF or Print")
    If response = vbYes Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & "Sheets.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 'False
    ElseIf response = vbNo Then
        ActiveSheet.PrintOut
    End If
 
Upvote 0
You could do this:

VBA Code:
    Dim response As Variant
    response = MsgBox("Click Yes to Save as PDF, or No to Print, or Cancel", vbYesNoCancel, "Save as PDF or Print")
    If response = vbYes Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & "Sheets.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 'False
    ElseIf response = vbNo Then
        ActiveSheet.PrintOut
    End If

Hello again,
When I choose to print on the printer, only sheet 1 is printed. If I choose PDF, all are printed in the same PDF. Do you know why?
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

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