Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subscript out of range when printing a worksheet 1

Status
Not open for further replies.

humbleprogrammer

Programmer
Oct 30, 2002
315
0
0
US
Hello,

I have a form with a combo box that allows you to select the worksheet you want to print. When choosing "ALL", it works correctly but when choosing a specific worksheet, it give a subscript out of range error. Below is my code. Any help is appreciated.

Code:
Private Sub btnPrint_Click()
    Dim PrintSheet As String
    PrintSheet = frmSelectSheet.cmbSelectSheet.Value
    If PrintSheet <> "ALL" Then
        Application.ScreenUpdating = False
        frmSelectSheet.Hide
        Sheets(" & PrintSheet & ").Activate
        Sheets(" & PrintSheet & ").PrintOut
        MsgBox PrintSheet & " was successfully printed.", vbInformation, "Successfully Printed"
        frmSelectSheet.Show
        Application.ScreenUpdating = True
    Else
        Application.ScreenUpdating = False
        frmSelectSheet.Hide
        Sheets("SURF WAX").Activate
        Sheets("SURF WAX").PrintOut
        Sheets("SURF ACCESS").Activate
        Sheets("SURF ACCESS").PrintOut
        Sheets("SNOW WAX").Activate
        Sheets("SNOW WAX").PrintOut
        Sheets("SNOW ACCESS").Activate
        Sheets("SNOW ACCESS").PrintOut
        Sheets("SOFTGOODS").Activate
        Sheets("SOFTGOODS").PrintOut
        Sheets("ALOE UP").Activate
        Sheets("ALOE UP").PrintOut
        Sheets("Solarez&ASSi").Activate
        Sheets("Solarez&ASSi").PrintOut
        MsgBox ("All worksheets were successfully printed."), vbInformation, "Successfully Printed"
        frmSelectSheet.Show
        Application.ScreenUpdating = True
    End If
    
End Sub

 
Hi humbleprogrammer,

The lines ..

Code:
[purple]        Sheets(" & PrintSheet & ").Activate
        Sheets(" & PrintSheet & ").PrintOut[/purple]

.. are trying to Activate and Printout a Sheet with the name between the quotes - [red]" & PrintSheet & "[/red]; it is not recognising your variable because it is inside quotes. You do not have a literal for the sheet name, you have a variable and all you need is ..

Code:
[blue]        Sheets(PrintSheet).Activate
        Sheets(PrintSheet).PrintOut[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top