I am trying to create a VBA script that will select multiple worksheets and then export those worksheets to PDF. I am still pretty new to coding, but I am fine with the coding for the PDF portion (got it to work on a single tab). Where I am having trouble is selecting multiple worksheets. I am using a dynamic array to look at the worksheet name and determine to select it or not. Every thing works fine until I get to the part where I select the worksheets. I get a Run-time error'9': Subscript out of range. I have put several Debug.Prints in my code and do see that my array contains worksheet names. Below is my code.
Sub pdf_Print()
Dim c As Integer
Dim d As Integer
Dim size As Integer
Dim i As Integer
Dim s As Integer
Dim wba As Workbook
Dim wa As Worksheet
Dim b As Integer
Set wba = ActiveWorkbook
'Debug prints active workbook name for Debugging
Debug.Print wba.Name
'Gets number of tabs to print
size = GetPrintTabs(wba)
'Setup Array for tabs to print
Dim Sheetstoprint() As Variant
ReDim Sheetstoprint(0 To size)
'Debug print Array size
Debug.Print size
'Stores tab names in Array
For Each wa In ActiveWorkbook.Worksheets
If (wa.Name Like "*segment*" And wa.Visible = True) Then
Sheetstoprint(i) = wa.Name
i = i + 1
Debug.Print Sheetstoprint(i)
End If
Next wa
'Debug to ensure show which tabs are in Array
For b = LBound(Sheetstoprint) To UBound(Sheetstoprint)
Debug.Print Sheetstoprint(b)
Next
'Select sheets in Array
ActiveWorkbook.Worksheets(Sheetstoprint).Select
End Sub
Public Function GetPrintTabs(awb As Workbook) As Integer
Dim wsa As Worksheet
Dim i As Integer
For Each wsa In awb.Worksheets
If (wsa.Name Like "*segment*" And wsa.Visible = True) Then
i = i + 1
End If
Next wsa
GetPrintTabs = i
Debug.Print "size =" & i
End Function