Worksheet names in an array getting run time error 9

lattechic

New Member
Joined
Jun 24, 2018
Messages
14
Hi guys,

I have a userform where I can select the worksheets I want to print as pdf. I have saved the names in an array (SheetsToPrint). However, when I get to the line ThisWorkbook.Sheets(Array(SheetsToPrint)).Select, I get run time error 9.

Please help. Thank you in advance.:biggrin:

Rich (BB code):
Private Sub cmdOK_click()


Dim SheetsToPrint() As Variant
Dim ws As Worksheet
Dim LastRow As Integer, i As Integer
Dim x As Integer: x = 2
Dim y As Long: y = 0
Dim printSheets As Variant


Application.ScreenUpdating = False


Set ws = Worksheets("Lookup")
LastRow = Cells(ws.Rows.Count, "R").End(xlUp).Row


'loop through each check box and save
For i = 1 To 22
            
    If Controls("CheckBox" & i).Value = True Then
        ws.Cells(x, 19) = "Y"
        y = y + 1
        ReDim Preserve SheetsToPrint(y)
        SheetsToPrint(y) = ws.Cells(x, 18)
        
    Else
        ws.Cells(x, 19) = "N"
    End If
    
    x = x + 1
    
Next i


ThisWorkbook.Sheets(Array(SheetsToPrint)).Select

ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=ActiveWorkbook.Path & "\Entry Form.pdf", _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False


Sheets("Start Express Parcels").Select


Unload Me
Application.ScreenUpdating = True




End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If SheetsToPrint is an array you shouldn't need Array.
Code:
ThisWorkbook.Sheets(SheetsToPrint).Select
 
Upvote 0
Hi Norie,

Thank you for the quick reply. Cleaned up the program a bit and tried it but I'm still getting the same error code.

Code:
Private Sub cmdOK_click()


Dim SheetsToPrint() As Variant
Dim ws As Worksheet
Dim i As Integer
Dim x As Integer: x = 2
Dim y As Long: y = 0
Dim printSheets As Variant


Application.ScreenUpdating = False


Set ws = Worksheets("Lookup")


'loop through each check box and save
For i = 1 To 22
            
    If Controls("CheckBox" & i).Value = True Then
        y = y + 1
        ReDim Preserve SheetsToPrint(y)
        SheetsToPrint(y) = ws.Cells(x, 18)
    End If
    
    x = x + 1
    
Next i


ThisWorkbook.Sheets(SheetsToPrint).Select




ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=ActiveWorkbook.Path & "\Entry Form.pdf", _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False


Sheets("Start Express Parcels").Select


Unload Me
Application.ScreenUpdating = True


End Sub
 
Upvote 0
Check that your sheet names are spelt correctly, including any leading/trailing spaces.
 
Upvote 0
Unless you've got an Option Base 1 statement at the top of the module, you've got an empty item 0 in your array. You should use:

Rich (BB code):
For i = 1 To 22
            
    If Controls("CheckBox" & i).Value = True Then
        ws.Cells(x, 19) = "Y"
        ReDim Preserve SheetsToPrint(y)
        SheetsToPrint(y) = ws.Cells(x, 18)
        y = y + 1
    Else
        ws.Cells(x, 19) = "N"
    End If
    
    x = x + 1
    
Next i

Note the position of the blue line.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top