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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA sheet number

Status
Not open for further replies.

ron513

Technical User
Mar 9, 2004
31
US

I need a VBA statement(s) that will supply a variable with the sheet number of a specific sheet. For example the sheet number of the sheet named Index.

Thanks

Ron
 




Hi,
Code:
MsgBox Sheets("Index").Index

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 




FYI: Keep in mind that the user can move sheets around, thus changing the relationship between sheet name and sheet index.

I much prefer using the Sheet.CodeName that one can ONLY change in the VB Editor.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
For my application would this be the correct statement?
Code:
ShtIndex = Sheets("Index").CodeName

Ron
 



How do you expect to use ShtIndex?

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Skip thanks for sticking with me.

My code will loop though all the sheets in the workbook starting from the sheet named Index to the last sheet, so I’m using the variable ShtIndex for the starting sheet of the loop.

Ron
 
Like this ?
For i = Sheets("Index").Index To Sheets.Count

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

PH, that works thanks.
I was also trying to get a little education on the Sheet.CodeName that Skip mentioned. It may not apply in this situation but just for future use. I plan on checking into it.

Ron
 
I plan on checking into it
When in the VBE feel free to play with the F2 and F1 keys ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

I’m a big fan of F1 but did not know about F2. I love this site!
Thanks again.

Ron
 




PHV's suggestio will work IF the order of the sheets NEVER CHANGE.

Another approch...
Code:
dim ws as worksheet
for each ws in worksheets
  select case ws.name
    case "Index","Include1","Include2"
      ' do something here
    case "Exclude1","Exclude2"
      ' maybe do nothing here
    case else
      ' do things there
  end select
next
You can list the sheets you want to include or list the sheets you want to exclude.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top