Referencing Worksheet From Formulas
Excel allows you to refer to cells on other worksheets, but these links are not relative; there is no way to refer to the next or previous sheet without hard coding that sheet name in the formula. And if the user changes the order of the sheets or inserts a sheet, the formula will no longer refer to the next or previous sheet. This page describes some VBA procedure that you can use to refer to the first, next, previous, or last worksheet in a workbook. These functions use the Application.Caller property, so they will not work unless they are called directly from worksheet cells. These functions don't use ActiveSheet or ActiveWorkbook. Instead, they go through the Parent objects of the Application.Caller properties. Therefore, they will work regardless of what the active workbook or worksheet happens to be, and regardless of whether the formulas themselves resided in the same workbook as the cells which call them. Returning The Number Of Worksheets In A Workbook The following function will return the number of worksheets in the workbook which is calling the function. Function SheetsCount() As Integer Returning The Index Of The Worksheet The following function will return the index (position) number of the worksheet which is calling the function. Function SheetPosition() As Integer Returning The Name Of The Current Worksheet The following function will return the name of the worksheet which is calling the function. Function ThisSheetName() As String Returning The Name Of The First Worksheet In The Workbook The following function will return the name of the first worksheet in the workbook which is calling the function. Function FirstSheetName() As String =INDIRECT(FirstSheetName()&"!A1") Returning The Name Of The Last Worksheet In The Workbook The following function will return the name of the last worksheet in the workbook which is calling the function. Function LastSheetName() As String =INDIRECT(LastSheetName()&"!A1") Returning The Name Of The Previous Worksheet The following function will return the name of the previous worksheet. If the function is called from a worksheet cell, the sheet name is enclosed in single quotes (apostrophes). If the function is not called from a cell, the name is not enclosed in quotes. Function PrevSheetName(Optional ByVal WS As Worksheet = Nothing) As String If this function is called from the first worksheet in a workbook, the name of the last worksheet will be returned. In other words, it will "loop" back around to the last worksheet, rather than returning an error. You can then use this name in an INDIRECT formula. For example to return the value of cell A1 from the previous worksheet, use =INDIRECT(PrevSheetName()&"!A1") Returning The Name Of The Next Worksheet The following function will return the name of the next worksheet. If the function is called from a worksheet cell, the sheet name is enclosed in single quotes (apostrophes). If the function is not called from a cell, the name is not enclosed in quotes.
=INDIRECT(NextSheetName()&"!A1") Getting The Value Of A Cell On The Previous Worksheet The following function will return the value of the specified cell on the previous worksheet. Addr is a string that may be either the address of a cell or the name of a defined name. Function RefOnPrevSheet(Addr As String) As Variant You can use this function to get the value of C5 on the previous worksheet: =RefOnPrevSheet("C5")
This function will return the name of a sheet based on its
position. If the function is called from a worksheet cell, and there is a space
in the sheet name, the sheet name is enclosed in single quotes (apostrophes). If
it is called by another VBA procedure, the sheet name is not enclosed in single
quotes. If you are calling this from another VBA procedure, you may specify a
workbook reference in which the sheet is located. Dim Q As String Dim S As String Application.Volatile True If IsObject(Application.Caller) = True Then S = Application.Caller.Parent.Parent.Worksheets(Ndx).Name If InStr(1, S, " ", vbBinaryCompare) > 0 Then Q = "'" Else Q = vbNullString End If Else S = IIf(WB Is Nothing, ActiveWorkbook, WB).Worksheets(Ndx).Name Q = vbNullString End If SheetNameOfIndex = Q & S & Q End Function
Getting The Value Of A Cell On The Next Worksheet The following function will return the value of the specified cell on the next worksheet. Addr is a string that may be either the address of a cell or the name of a defined name. Function RefOnNextSheet(Addr As String) As Variant =RefOnNextSheet("C5") Note that the "C5" is passed in quotes, as a string, rather than a range reference. |
||
|
||
|