Print selected sheets as separate PDFs

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I have been trying to write a macro that when run will print each sheet in a selection to PDF and name each PDF the name of the sheet. For example, if I have selected Sheet 1, Sheet 2 and Sheet 3 in a workbook. I want a macro that when executed will print Sheet1 to PDF and name the PDF "Sheet 1". Then it will print Sheet 2 to PDF and name the PDF "Sheet 2". Then it will print Sheet 3 to PDF and name the PDF "Sheet 3".

Here is what I have been trying but it doesn't work:

VBA Code:
Sub ExportPDFSelectionSheets()
    Dim ws As Worksheet
    Dim Path As String
    Dim Name As String
    
    Path = "H:\(H) Sandbox\(H) Scans\"
    
    For i = 1 To ActiveWindow.SelectedSheets.Count
        Set ws = ActiveWindow.SelectedSheets(i)
        Name = ws.Name
        
        ws.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=Path & Name & ".pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    Next i
End Sub

I feel like this shouldn't be hard but it is stumping me. Please help!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this:
VBA Code:
Public Sub Save_Selected_Sheets_As_PDFs()

    Dim Path As String
    Dim ws As Worksheet
    
    Path = "H:\(H) Sandbox\(H) Scans\"
    
    For Each ws In ActiveWindow.SelectedSheets
        ws.Select
        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & ws.Name & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
    
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Public Sub Save_Selected_Sheets_As_PDFs()

    Dim Path As String
    Dim ws As Worksheet
   
    Path = "H:\(H) Sandbox\(H) Scans\"
   
    For Each ws In ActiveWindow.SelectedSheets
        ws.Select
        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & ws.Name & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next
   
End Sub
This works perfectly! Thank you!!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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