VBA Printing Multiple Worksheets using Array

CM214

New Member
Joined
May 12, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a macro to print out multiple worksheets (in a particular order) into a pdf. I was trying to specify an array of worksheets via their Codename rather than the normal tab name but I keep running into a run time error '9' Subscript out of range. Below is the code: Some of the worksheets have two pages to print out, others have only one. Any help would be greatly appreciated.

VBA Code:
Dim theArray As Variant

        theArray = Array("Sheet4", "Sheet2", "Sheet1", "Sheet3")
        Worksheets("theArray").Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=ActiveWorkbook.Path & Application.PathSeparator & _
            "test.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi, try the below. I think I had to save the macro first before it would create the pdf.

VBA Code:
Option Explicit
Sub forPrintingSheetstoPDF()
    Dim theArray() As Variant, Sheet As Variant
    
    On Error Resume Next
    
    theArray = Array("Sheet4", "Sheet2", "Sheet1", "Sheet3")
        
    For Each Sheet In theArray
        Sheets(Sheet).Move after:=Sheets(Sheets.Count)
    Next Sheet
             
    ThisWorkbook.Sheets(theArray).Select
             
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=ActiveWorkbook.Path & Application.PathSeparator & _
            "test.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,964
Messages
6,133,772
Members
449,831
Latest member
Watever

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