Set Array Name in a Cell and Reference in Code

vandango05

Board Regular
Joined
Oct 6, 2008
Messages
110
Good day all.

I'm using the code below to be able to create a pdf from multiple worksheets with different print areas as one file. The issue I have is that the array which determines which sheets to print as well as the print areas are different each time. It will be fairly easy for me to create a formula to create the array and print areas in a worksheet called "Summary" (Sheet32) in say cells A1 for the Array and A2 for the print area.

Is there a way i can reference the values in these cells in the code?



Code:
Sub printmultiplesheets()
    Dim path As String
    Dim myArr As Variant, a As Variant
    Dim rngArr As Variant
    Dim Ws As Worksheet
    Dim i As Integer
    


    myArr = Array("Contract 1st Page", "Windows", "Doors") '<~~ Sheet names
    
    rngArr = Array("C1:K57", "B2:BX95", "B2:BX111") '<~~ print area addresses


    For i = 0 To UBound(myArr)
        Set Ws = Sheets(myArr(i))
        With Ws
            .PageSetup.PrintArea = .Range(rngArr(i)).Address
        End With
    Next
    Sheets(myArr).Select


    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Sheet32.Range("S18").Value _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True




End Sub
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What exactly will be in A1 and A2?
 
Upvote 0
Here a possible method - just put the sheet names and their ranges as plain text separated by a comma i.e. don't use double quotes when entering them into cell A1 or A2 (refer my comments in the following code as well):

Code:
Option Explicit
Sub Macro1()

    Dim strSheetArray() As String
    Dim strRangeArray() As String
    Dim intArrayIndex As Integer
    Dim Ws As Worksheet
    
    Application.ScreenUpdating = False
    
    strSheetArray() = Split(Sheet32.Range("A1"), ",") '<~~ Sheet names like so: Contract 1st Page, Windows, Doors
    strRangeArray() = Split(Sheet32.Range("A2"), ",") '<~~ print area addresses like so: C1:K57, B2:BX95, B2:BX111
    
    For intArrayIndex = LBound(strSheetArray) To UBound(strSheetArray)
        Set Ws = ThisWorkbook.Sheets(strSheetArray(intArrayIndex))
        With Ws
            .PageSetup.PrintArea = .Range(strRangeArray(intArrayIndex)).Address
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=Sheet32.Range("S18").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        End With
    Next intArrayIndex
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
What exactly will be in A1 and A2?

It will identify which sheets to print and the print areas, it can be displayed in any way that would be suitable for example:

A1 - Contract 1st Page, Windows, Doors or Sheet5, Sheet6, Sheet7 or "Contract First Page", "Windows", "Doors"

A2 C1:K57, B2:BX95, B2:BX111 or "C1:K57", "B2:BX95", "B2:BX111"

or if it needs to be displayed a different way I can do this.
 
Upvote 0
Thanks for the replies. Code is stopping at Set Ws = ThisWorkbook.Sheets(strSheetArray(intArrayIndex))

In A1 i've got this Contract 1st Page, Windows, Doors and in A2 this C1:K57, B2:BX95, B2:BX111

Despite it not running it was still generating a PDF with just the "Contract 1st Page" Worksheet
 
Upvote 0
I think you'll need to be careful with trailing/leading spaces if you are going to use a delimited list in a cell.

For example, from your list the second sheet name looks more like ' Windows' rather than 'Windows' and if you don't have a sheet named ' Windows' then you'll get a subscript out of range error on the line you've indicated.
 
Upvote 0
Thank you for your help so far. That indeed was the issue however its not quite working how I need it to. The original code created an array which then selected all worksheets within the array before saving them as PDF so it would be one file. The changed code will create separate PDF's for each worksheet.
 
Upvote 0
Try this:

Code:
Option Explicit
Sub Macro2()

    Dim strSheetArray() As String
    Dim strRangeArray() As String
    Dim strSheetsArray() As String
    Dim intArrayIndex As Integer
    Dim Ws As Worksheet
    
    Application.ScreenUpdating = False
    
    strSheetArray() = Split(Sheet32.Range("A1"), ",") '<~~ Sheet names like so: Contract 1st Page, Windows, Doors
    strRangeArray() = Split(Sheet32.Range("A2"), ",") '<~~ print area addresses like so: C1:K57, B2:BX95, B2:BX111
    
    For intArrayIndex = LBound(strSheetArray) To UBound(strSheetArray)
        Set Ws = ThisWorkbook.Sheets(Trim(strSheetArray(intArrayIndex)))
        Ws.PageSetup.PrintArea = Range(Trim(strRangeArray(intArrayIndex))).Address
        ReDim Preserve strSheetsArray(0 To intArrayIndex)
        strSheetsArray(intArrayIndex) = Trim(strSheetArray(intArrayIndex))
    Next intArrayIndex
    
    Sheets(strSheetsArray).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Sheet32.Range("S18").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Trebor76

Thank you for your help, initially had some issues but this appears to be down to my list created.

I also had to change the print at the end but now works perfectly. Thanks again.

Code:
Sub Macro2()

    Dim strSheetArray() As String
    Dim strRangeArray() As String
    Dim strSheetsArray() As String
    Dim intArrayIndex As Integer
    Dim Ws As Worksheet
    
    Application.ScreenUpdating = False
    
    strSheetArray() = Split(Sheet32.Range("S23"), ",") '<~~ Sheet names like so: Contract 1st Page, Windows, Doors
    strRangeArray() = Split(Sheet32.Range("S24"), ",") '<~~ print area addresses like so: C1:K57, B2:BX95, B2:BX111
    
    For intArrayIndex = LBound(strSheetArray) To UBound(strSheetArray)
        Set Ws = ThisWorkbook.Sheets(Trim(strSheetArray(intArrayIndex)))
        Ws.PageSetup.PrintArea = Range(Trim(strRangeArray(intArrayIndex))).Address
        ReDim Preserve strSheetsArray(0 To intArrayIndex)
        strSheetsArray(intArrayIndex) = Trim(strSheetArray(intArrayIndex))
    Next intArrayIndex
    
    Sheets(strSheetsArray).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Sheet32.Range("S18").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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