Hello All,
I am trying to select sheets to generate a PDF by using a UserForm with checkboxes so the user can select which sheets he/she wants generated into a PDF. Below is my code and I feel as if I am close but I continue to get a "Run-time error '9': Subscript out of range" at the "Sheets(ary).Select" line near the bottom of my code. Any and all help is appreciated and thank you in advance for your help.
I am trying to select sheets to generate a PDF by using a UserForm with checkboxes so the user can select which sheets he/she wants generated into a PDF. Below is my code and I feel as if I am close but I continue to get a "Run-time error '9': Subscript out of range" at the "Sheets(ary).Select" line near the bottom of my code. Any and all help is appreciated and thank you in advance for your help.
Code:
Private Sub chbxEnter_Click()
Dim PDFsheets As String
Dim s As Worksheet
PDFsheets = "Sheet10,Sheet15,Sheet6,Sheet5,Sheet4,Sheet14,Sheet11,Sheet12,Sheet17,Sheet13,Sheet2"
ary = Split(PDFsheets, ",")
If CheckBox1.Value = True Then
PDFsheets = "Sheet10"
End If
If CheckBox2.Value = True Then
If PDFsheets = "" Then
PDFsheets = "Sheet15"
Else
PDFsheets = PDFsheets & ",Sheet15"
End If
End If
If CheckBox3.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet6
Else
PDFsheets = PDFsheets & ",Sheet6"
End If
End If
If CheckBox4.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet4
Else
PDFsheets = PDFsheets & ",Sheet4"
End If
End If
If CheckBox5.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet5
Else
PDFsheets = PDFsheets & ",Sheet5"
End If
End If
If CheckBox6.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet14
Else
PDFsheets = PDFsheets & ",Sheet14"
End If
End If
If CheckBox7.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet11
Else
PDFsheets = PDFsheets & ",Sheet11"
End If
End If
If CheckBox8.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet12
Else
PDFsheets = PDFsheets & ",Sheet12"
End If
End If
If CheckBox9.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet17
Else
PDFsheets = PDFsheets & ",Sheet17"
End If
End If
If CheckBox10.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet13
Else
PDFsheets = PDFsheets & ",Sheet13"
End If
End If
If CheckBox11.Value = True Then
If PDFsheets = "" Then
PDFsheets = Sheet2
Else
PDFsheets = PDFsheets & ",Sheet2"
End If
End If
Sheets(ary).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\TestFolder\Book1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub