Subscript Out of Range

R

RK

Pls. help! What's wrong with this code? This code is executed when the user
clicks the command button on the UserForm1. It print-previews the selected
worksheets.


Private Sub CommandButton1_Click()
For i = 1 To 13
If UserForm1.Controls("Checkbox" & i).Value = "True" Then
Worksheets("Sheet" & i).PrintPreview
End If
Next i
Unload Me
End Sub


VBA flags the 4th line (.PrintPreview) as "Subscript out of Range". Am I
missing a Dim statement?

Thanks.
 
C

Chip Pearson

You get a "subscript out of range" if you don't have a sheet
named Sheeti, where i is the number. Are you sure you have a
worksheet with that name?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
R

RK

You are right. I don't have a sheet by that name. How do I print-preview the
selected sheets by using a for-next loop instead of referring to the sheet's
name?
 
F

Frank Kabel

Hi
you may try the following (using the index property):
Private Sub CommandButton1_Click()
For i = 1 To 13
If UserForm1.Controls("Checkbox" & i).Value = "True" Then
Worksheets(i).PrintPreview
End If
Next i
Unload Me
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top