简体   繁体   中英

Printing multiple excel worksheets to one pdf using VBA and getting Run-time error '9': subscript out of range when selecting tabs

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

I think you may have over-complicated things in your quest of not quite getting the solution.

This works for me:

Sub SelectMultipleSheets()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    Dim SheetsToPrint() As Variant
    Dim i As Integer

    If (ws.Name Like "*segment*" And ws.Visible = True) Then

        ReDim Preserve SheetsToPrint(i)
        SheetsToPrint(i) = ws.Name
        i = i + 1

    End If

Next

Worksheets(SheetsToPrint).Select


End Sub

The other answers are probably a better way to go about it, but for the sake of completeness I'll explain what's happening in your code.

You set the size variable to equal the number of sheets that contain the word 'segment' in the workbook. In my test case, I had 3 sheets (of 7 total) that contained the word. The size variable, therefore, gets set to 3. When you dimension the array Sheetstoprint from 0 to 3 you are allowing a total of 4 sheet names to be held (index 0, index 1, index 2, and index 3) despite only having three sheets that match the criterion. Or more generally if you have n sheets named like 'segment' you are dimensioning an array of size n+1.

To correct this I changed

ReDim Sheetstoprint(0 To size)

To

ReDim Sheetstoprint(1 To size)

And then to compensate I had to re-order the i variable farther down in the code, so I changed this:

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

To:

For Each wa In ActiveWorkbook.Worksheets
   If (wa.Name Like "*segment*" And wa.Visible = True) Then
   i = i + 1
   Sheetstoprint(i) = wa.Name
   Debug.Print Sheetstoprint(i)
 End If
Next wa

And it seems to work fine by selecting the sheets I would expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM