Need some help with VBA loop/referencing, please.

judgepax

Board Regular
Joined
Jan 20, 2009
Messages
53
BIG PICTURE: Create a script that at the touch of a button will consistently format and save each worksheet as its own .pdf.

WHAT I HAVE SO FAR: I have a working VBA for the formatting and a working VBA for the "Save As... pdf", but I am stuck on how to properly do a VBA reference loop. I have the following script refencing a simple "SheetList" (name of every tab in the workbook). I want the script to repeat for Sheets 1 through x but I can't figure out how to have the same two lines of code repeat until VBA finishes last x sheet. Possibly something with a SheetCount?

Code:
Sub SaveAspdf()
 
    Sheets(1).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Desktop\" & Worksheets("SheetList").Range.Cells(1,1), Quality:=xlQualityStandard, OpenAfterPublish:=False
 
    Sheets(2).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Desktop\" & Worksheets("SheetList").Range.Cells(1,2), Quality:=xlQualityStandard, OpenAfterPublish:=False
 
    Sheets(3).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Desktop\" & Worksheets("SheetList").Range.Cells(1,3), Quality:=xlQualityStandard, OpenAfterPublish:=False
 
End Sub

VBA is not my strong point, so I appreciate any help I can get.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Something like this? (Change the directory t"C:\desktop'" to your chosen path):
Code:
Sub TestMakePdf()
Dim WS As Worksheet
    For Each WS In ActiveWorkbook.Worksheets
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="C:\Desktop\" & WS.Name, Quality:=xlQualityStandard, OpenAfterPublish:=False
    Next WS
End Sub

Hope that helps,
Cindy
 
Upvote 0
Oops...please ignore my previous post it creates output, but all the pdf's were the same (but with different file names). If your desktop is within "Documents and Settings\username" you will need to change the path or you'll get an error.
This should work:
Code:
Sub TestMakePdf()
Dim WS As Worksheet
    For Each WS In ActiveWorkbook.Worksheets
        WS.ExportAsFixedFormat Type:=xlTypePDF, FileName:="C:\Desktop\" & WS.Name, Quality:=xlQualityStandard, OpenAfterPublish:=False
    Next WS
End Sub
 
Upvote 0
WOW! That worked better than I was aiming for!! Thank you so much!! I now have a slick-as-snot cheat macro for a previously very labor-intense process.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
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