Question for Jim Thomlinson

B

Brad

Thank you for pointing out that I should use the code name - rather than tab
name - how would the following code be change to use code name - rather than
tab name?

Sub Print_pages()
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> "Input" And sht.Visible = True Then
sht.PrintOut
End If
Next
End Sub
 
J

Jim Thomlinson

That code is just fine. At no point does it rely on the tab name of the
sheets in your workbook.

In the excel object hierarchy there is the Worksheet object and the
Worksheets collection (note the s in the latter object). What your code is
doing is it is taking a worksheet object and using that to traverse the
worksheets collection. At no time are you refering to the sheets by their tab
name which is dangerous since user have access to change the tab names.
 
B

Brad

Okay - I changed sheet1 which was the input sheet to shtInput

Then
Sub Print_pages()
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> "shtInput" And sht.Visible = True Then
sht.PrintOut
End If
Next
End Sub

Prints out the input page and the two output pages

If I make the code
Sub Print_pages()
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> shtInput And sht.Visible = True Then
sht.PrintOut
End If
Next
End Sub

I get an error message
 
T

Tom Ogilvy

that is because shtInput probably isn't the code name of the sheet with the
tab name of "ShtInput"

go into the project explorer in the VBE (alt+F11). In the list worksheets,
you will probably see

Sheet3 (ShtInput)

use whatever value I have portrayed here as Sheet3.
 
B

Brad

Tom,

Jim suggested that I go into the properties window in VBE and rename the
worksheet from sheet1 to something that describes the tab more. I did not
change the tab name, that remained the same. The reason that he suggested
doing this the my macros, that I've set up, should used the code name not the
tab name (because users can change the tab name). I was able to change the
code name in the properties section - but having difficulties making the
macro's work.

Does this help???
 
J

Jim Thomlinson

Give this a try...

Sub Print_pages()
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> shtInput.Name And sht.Visible = True Then
sht.PrintOut
End If
Next
End Sub

ShtInput refers directly to the sheet object. What you need to do is to
check the name property of sht against the name property of shtInput...
 
B

Brad

And the idiot of the hour award goes to me.

Thanks Jim, for being kind to a rookie - But I should have known better.
 

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