relative sheet reference [Solved]

Corticus

Well-known Member
Joined
Apr 30, 2002
Messages
1,579
Hi all,

Is there a way to refer to a sheet by number, say sheet1, in a formula, regardless of what the sheet is named?

No luck with search...

Thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Here's three way to refer to a sheet
Method 1 and 3 does not change when you change the sheet name

Public Sub selectsheet()
Sheet1.Select
Sheets("Sheet1").Select
Sheets(1).Select

End Sub


A sheet in a workbook is normally referred to by specifying the Sheets collection and the name or the index number of the sheet. The following is the syntax of the this command:


Sheets("sheet name")
or Sheets(number)

For example, Sheets("Sheet1") specifies the sheet named Sheet1 in the active workbook. If Sheet1 were the first sheet in a file, then Sheets(1) would also refer to Sheet1 in the active workbook. If the sheets are moved or sheets added or deleted, the number that refers to a particular sheet can change.

To specify a sheet in a different workbook, the statement Workbooks("filename") can be used as the object qualifier. For example:

Workbooks("Personal.Xls").Sheets("Sheet1")

specifies sheet Sheet1 in Personal.Xls, no matter what workbook or sheet is currently active.

The Sheets collection consists of all types of sheets - worksheets, charts, dialogsheets, and Microsoft Excel 4 macro sheets. The collections Worksheets, Charts, Dialogs, Microsoft Excel4MacroSheets can also be used to specify a sheet:


Worksheets("Sheet1")

or Worksheets(1)

The Count property can be used to find out how many of a particular type sheet there are in a file:


MsgBox "There are " & Charts.Count & " chart sheets."

The following macro displays how many worksheets there are in the active file and then displays each one's name and index number.

Sub DisplayWorksheetNamesAndNumber()
Dim num As Integer, I As Integer
'Determine number of worksheets and display message
num = Worksheets.Count
MsgBox "There are " & num & _
" worksheets in this file"

'Display the number and name of each worksheet
For I = 1 To num
MsgBox "Worksheet " & I & " is called " & _
Worksheets(I).Name
Next I
End Sub


You can also use the keyword ActiveSheet to refer to the active sheet. For example if the active sheet is named "Survey Results", the following two statements both refer to this sheet:


Sheets("Survey Results")

or ActiveSheet
 
Upvote 0
Thanks for the detailed response Nimrod,

Without sounding ungrateful, I know how to do this in code. I would like to refer to the sheet number in a formula.

I was thinking a combination of the getinfo() UDF to pull the sheet names to a table, and then indirect() to use those sheetnames in the formulas, but I'm not sure there isn't a better way.

edit: reason for this, I have formulas refering to sheets that have not yet been renamed from Sheet1 to whatever they will be.

Thanks again!
 
Upvote 0
One more quickie...

Does anybody know how to make this UDF recalculate if the user adds a sheet, or changes the name of one, or really anything that would cause the table above's values to need to be updated?

I have a consolidated data sheet, that pulls all the data from any tabs in the workbook. I want to be able to account for the user changing the tab names, thus indirect() and getinfo().

Thanks again!
 
Upvote 0
Thanks Aladin,

I think Sheetname() might be from an add-in, perhaps morefunc, because I got #name. I would prefer to use the Getinfo() UDF because I can 'attach' the function to the worksheet, which I don't think anybody know how to do with add-ins.

I would really like to see some of the morefunc functions developed as open source UDFs, so I could send them with my spreadsheets to clients and such. Oh well.

Thank you, though!
 
Upvote 0
Solved

Okay,

If you add:

application.volatile
(thanks NateO!)
to beginning of the function code, Excel will force the UDF to recalculate when you change formats, rename sheets, etc. This solved my dillema.

BTW, the getinfo() UDF is here:

http://www.cpearson.com/excel/GetInfo.htm

Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,215,243
Messages
6,123,837
Members
449,129
Latest member
krishnamadison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top