Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Brandon
 
Posts: n/a
Default Return number of current sheet

I'm looking for a way to create a function to return the current sheet youre
working in. Not its name (Ive found that function), but the number of the
sheet. Example: if you have a total of 8 sheets, and youre working on the
3rd sheet, Id like to return the number 3 in a certain cell on the third
sheet, 4 on the fourth sheet, 5 on the fifth sheet, etc., and have those
numbers update if sheets are added or deleted. Is that possible?


  #2   Report Post  
JE McGimpsey
 
Posts: n/a
Default

One way:

Public Function SheetNum(Optional rng As Range) As Variant
Application.Volatile
If rng Is Nothing Then
If TypeName(Application.Caller) = "Range" Then
Set rng = Application.Caller.Cells
Else
SheetNum = CVErr(xlErrRef)
Exit Function
End If
End If
SheetNum = rng.Parent.Index
End Function

usage:

=SheetNum()

or

=SheetNum(MySheet!A1)


note that this will not automatically update when a sheet is moved
within a workbook - Application.Volatile will cause it to update the
next time a calculation is made.


In article ,
"Brandon" wrote:

I'm looking for a way to create a function to return the current sheet youre
working in. Not its name (Ive found that function), but the number of the
sheet. Example: if you have a total of 8 sheets, and youre working on the
3rd sheet, Id like to return the number 3 in a certain cell on the third
sheet, 4 on the fourth sheet, 5 on the fifth sheet, etc., and have those
numbers update if sheets are added or deleted. Is that possible?

  #3   Report Post  
Brandon
 
Posts: n/a
Default

Thanks very much. That worked just fine!

"JE McGimpsey" wrote:

One way:

Public Function SheetNum(Optional rng As Range) As Variant
Application.Volatile
If rng Is Nothing Then
If TypeName(Application.Caller) = "Range" Then
Set rng = Application.Caller.Cells
Else
SheetNum = CVErr(xlErrRef)
Exit Function
End If
End If
SheetNum = rng.Parent.Index
End Function

usage:

=SheetNum()

or

=SheetNum(MySheet!A1)


note that this will not automatically update when a sheet is moved
within a workbook - Application.Volatile will cause it to update the
next time a calculation is made.


In article ,
"Brandon" wrote:

I'm looking for a way to create a function to return the current sheet you€„¢re
working in. Not it€„¢s name (I€„¢ve found that function), but the number of the
sheet. Example: if you have a total of 8 sheets, and you€„¢re working on the
3rd sheet, I€„¢d like to return the number 3 in a certain cell on the third
sheet, 4 on the fourth sheet, 5 on the fifth sheet, etc., and have those
numbers update if sheets are added or deleted. Is that possible?


  #4   Report Post  
Harlan Grove
 
Posts: n/a
Default

"JE McGimpsey" wrote...
....
If TypeName(Application.Caller) = "Range" Then
Set rng = Application.Caller.Cells

....

Why TypeName(x) = "y" rather than TypeOf x Is y? Also, when the condition is
true, won't Application.Caller.Parent resolve to the same worksheet as
Application.Caller.Cells.Parent? If so, why use .Cells?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
make an excel worksheet (sheet 2) open w/ the cursor located in t. animated text in excel worksheet Excel Discussion (Misc queries) 0 February 10th 05 10:29 PM
Return Multiple Results with Lookup Josh O. Excel Worksheet Functions 1 February 4th 05 08:07 PM
Find all text instances in a sheet and add one number from each row Greg Excel Discussion (Misc queries) 1 January 31st 05 11:45 PM
Copy formula...sheet 2 sheet Mick New Users to Excel 0 January 26th 05 01:58 AM
Function to automatically insert a new sheet as a result of data entry? Mark Mulik Excel Worksheet Functions 2 November 28th 04 02:21 AM


All times are GMT +1. The time now is 11:24 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"