I'm a moderate VBA coder, meaning not a beginner, but I also wouldn't call myself an expert. I've hit upon a problem I can't figure out.
I am going to be creating a userform with checkboxes, where the user will be able to put a check next to the name of the sheets that he/she wants to include when generating a single PDF file. Since it is unknown which ones they will select, I want to have my code use a string variable to define the array that will select the correct sheets. Here is the code I have written to test it out.
If I remove this line:
the sub works, and it generates a PDF of Page 2 only. But with the above line in there, I get a Run-time Error 9: subscript out of range on the line that creates the array. I can't figure out what I'm doing wrong.
I've searched a few forums about this, but haven't had any luck finding a solution. Any help would be greatly appreciated.
I am going to be creating a userform with checkboxes, where the user will be able to put a check next to the name of the sheets that he/she wants to include when generating a single PDF file. Since it is unknown which ones they will select, I want to have my code use a string variable to define the array that will select the correct sheets. Here is the code I have written to test it out.
Code:
Sub GeneratePDF()
Dim Filename As String
Dim SheetsToPrint As String
Filename = "TempFile"
SheetsToPrint = "Page 2"
SheetsToPrint = SheetsToPrint & ", " & "Page 3"
Sheets(Array(SheetsToPrint)).Select
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "/" & Filename & ".pdf", , , False
End Sub
If I remove this line:
Code:
SheetsToPrint = SheetsToPrint & ", " & "Page 3"
I've searched a few forums about this, but haven't had any luck finding a solution. Any help would be greatly appreciated.
Last edited: