Link to home
Start Free TrialLog in
Avatar of Michael
MichaelFlag for United States of America

asked on

Excel VBA array from dynamic cell list

We writing a script that will print multiple Excel sheets to a single PDF.  We are dynamically creating the list of sheets to print using one of the sheets and checking cells in the list.  The corresponding values of cells which have been checked are copied to a new list of cells starting at AA1.  We are using list starting at AA1:AA? to create an array that tells the VBA code which sheets to print.

Our challenge is the list is a different length every time; it may end at AA5 one time and AA250 another.  How do we make the range change as appropriate?  (For the Values dimension)

Sub PrintEGAM2()
'
' PrintEGAM2 Macro
' Print with different Macro
'
Dim wbThis As Workbook
Dim wsInfo As Worksheet
Dim sPDFName As String
Dim Values As Variant

Set wbThis = Application.ThisWorkbook
Set wsInfo = wbThis.Worksheets("Print sheet")

'/// Change the output file name here!///
    sPDFName = InputBox("Enter filename", "Print Quote")
    If sPDFName = "" Then Exit Sub

'Delete the PDF if it already exists
If Dir(ActiveWorkbook.Path & "\" & sPDFName & ".pdf") = sPDFName & ".pdf" Then
    SetAttr ActiveWorkbook.Path & "\" & sPDFName & ".pdf", vbNormal
    'Kill sPDFPath & sPDFName & ".pdf"
End If
    
Values = Application.Transpose([AA1:AA5])

'Select the multiple sheets
Sheets(Values).Select

'Set Filename
stFileName = ActiveWorkbook.Path & "\" & sPDFName

'Print all to PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
       stFileName _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
       :=True, OpenAfterPublish:=False

'Unselect the three worksheets, otherwise Excel will not allow you to change anything on the three worksheets
Sheets("Print Sheet").Select

'
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Glenn Ray
Glenn Ray
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial