Loop Save-as PDF through workbook

laskisperj

New Member
Joined
Jun 18, 2008
Messages
3
I'm currently using the following macro to save an active worksheet as a pdf:


Sub SaveAsPDF()

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Documents and Settings\My Documents\" & Range("a1").Value & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True


End Sub



I want this to loop through each of the worksheets in the worksbook to create a different pdf file for each worksheet, but keep getting errors...i think with "Activesheet"

I tried using:

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The following does what you ask for me in excel 2007. It will create pdf's of each worksheet and then return the user to the original active sheet. It is probably not the most efficient answer, as I am new to VBA, but it does work.

Code:
Sub PDFENTIREWORKBOOK()
 
Dim ws As Worksheet, aws As Worksheet
On Error GoTo Err
'set active worksheet to return to later
Set aws = ActiveSheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Jason\Documents\" & ws.Range("A1") & ".pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
Next ws
aws.Activate
Application.ScreenUpdating = True
Exit Sub
Err:
Application.ScreenUpdating = True
MsgBox ("Failure")

Jason

Edit: Be sure to change the filepath!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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