Save multiple pages to PDF

Peter Muller

Board Regular
Joined
Oct 15, 2018
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I save every page to the right of “Reports” to a PDF file.

I have the following code which you assisted me with before, but it was to save each page individually. I now want to save all the pages to PDF at the same time. Please assist.

'To print sheet and save to PDF

Dim sht_nm As String

Dim dt As String

sht_nm = ActiveSheet.Name

dt = Format(Now(), " DD-MMM-YY")


'Save PDF Report to Folder

.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _

"C:\Users\Peter\Desktop\Finance\PDF Reports\" & sht_nm & dt & ".pdf", Quality:=xlQualityStandard, _

IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _

False


I have this code to print each sheet to the right of “Reports” which I have tried to marry to the above code but cannot get it to work. Please help



PrintSheets()

Dim idx, i As Long

idx = Sheets("Reports").Index + 1

For i = idx To Sheets.Count

Sheets(i).PrintOut

Next
 
Good afternoon,

Hope I am posting this follow up question correctly seeing that i already received an answer to my original question.

If I only want certain columns to display on the saved PDF doc, e.g., columns A-I only, where the source sheet extends to column AE, where in the code you gave me will I insert this instruction?

Example:

Columns("J:AO").EntireColumn.Hidden = True

And

Columns("A:A").ColumnWidth = 1.43

Then after saving the sheet, to unhide those columns again?
No worries.
VBA Code:
Public Sub Save_Each_Sheet_As_PDF()

    Dim folder As String
    Dim currentSheet As Worksheet
    Dim i As Long
       
    folder = "C:\Users\Peter\Desktop\Finance\PDF Reports\"
   
    With ActiveWorkbook
        Set currentSheet = .ActiveSheet
        For i = .Sheets("Reports").Index + 1 To .Sheets.Count
            .Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, Filename:=folder & .Sheets(i).Name & Format(Now, " DD-MMM-YY") & ".pdf", _
                Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Next
        currentSheet.Select
    End With
    MsgBox "Done"
   
End Sub
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If I only want certain columns to display on the saved PDF doc, e.g., columns A-I only, where the source sheet extends to column AE, where in the code you gave me will I insert this instruction?

Example:

Columns("J:AO").EntireColumn.Hidden = True

And

Columns("A:A").ColumnWidth = 1.43

Then after saving the sheet, to unhide those columns again?
Macro updated as requested. Also changed to loop through Worksheets instead of Sheets, because your columns request means we aren't dealing the Chart sheets.
VBA Code:
Public Sub Save_Each_Sheet_As_PDF()

    Dim folder As String
    Dim currentSheet As Worksheet
    Dim i As Long
      
    folder = "C:\Users\Peter\Desktop\Finance\PDF Reports\"
  
    With ActiveWorkbook
        Set currentSheet = .ActiveSheet
        For i = .Worksheets("Reports").Index + 1 To .Worksheets.Count
            .Worksheets(i).Columns("J:AO").EntireColumn.Hidden = True
            .Worksheets(i).Columns("A").ColumnWidth = 1.43
            .Worksheets(i).ExportAsFixedFormat Type:=xlTypePDF, Filename:=folder & .Worksheets(i).Name & Format(Now, " DD-MMM-YY") & ".pdf", _
                Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            .Worksheets(i).Columns("J:AO").EntireColumn.Hidden = False
        Next
        currentSheet.Select
    End With
    MsgBox "Done"
  
End Sub
More code is needed if you want the macro to restore the column widths.
 
Last edited:
Upvote 0
Solution
Macro updated as requested. Also changed to loop through Worksheets instead of Sheets, because your columns request means we aren't dealing the Chart sheets.
VBA Code:
Public Sub Save_Each_Sheet_As_PDF()

    Dim folder As String
    Dim currentSheet As Worksheet
    Dim i As Long
     
    folder = "C:\Users\Peter\Desktop\Finance\PDF Reports\"
 
    With ActiveWorkbook
        Set currentSheet = .ActiveSheet
        For i = .Worksheets("Reports").Index + 1 To .Worksheets.Count
            .Worksheets(i).Columns("J:AO").EntireColumn.Hidden = True
            .Worksheets(i).Columns("A").ColumnWidth = 1.43
            .Worksheets(i).ExportAsFixedFormat Type:=xlTypePDF, Filename:=folder & .Worksheets(i).Name & Format(Now, " DD-MMM-YY") & ".pdf", _
                Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            .Worksheets(i).Columns("J:AO").EntireColumn.Hidden = False
        Next
        currentSheet.Select
    End With
    MsgBox "Done"
 
End Sub
More code is needed if you want the macro to restore the column widths.
Thank you so much John, this is perfect ?:)?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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