Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Relative Worksheet References.
Written by Allen Wyatt (last updated February 4, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Suppose you have a workbook with three worksheets, Sheet1, Sheet2 and Sheet3. In column A1 of worksheet Sheet2 you have the formula =Sheet1!A1. When you copy that formula from Sheet2 to cell A1 of Sheet3, the formula still references Sheet1. How can that be, though? Why doesn't Excel adjust the sheet reference, like it does the cell references?
Like named ranges, Excel treats worksheet names as absolute. Each worksheet object is independent of all other worksheets in the workbook. When you paste a formula that includes a sheet reference, that sheet reference is left unchanged in what is pasted.
There are a couple of things you can do. One is to simply modify the formula reference after it is pasted so that it references the correct sheet. If you have many of them to change, then you can select all the formulas in the target worksheet (F5 | Special | Formulas) and then use Find and Replace to replace the original worksheet name (Sheet1) with the correct worksheet name (Sheet2).
If your referencing needs are not complex, then you can use a macro approach. For instance, if you want a formula in a particular cell to refer to a cell on the sheet previous to the current sheet, then you can do that by macro rather easily. Consider the following macro:
Function PrevSheet(rCell As Range) Application.Volatile Dim i As Integer i = rCell.Cells(1).Parent.Index PrevSheet = Sheets(i - 1).Range(rCell.Address) End Function
The macro looks at the current worksheet and then figures out which worksheet is before it. The reference is then made for that worksheet. Once you've created the PrevSheet macro, here's one way the function can be used in a cell:
=PrevSheet(A1)
This returns the value of cell A1 from the previous worksheet. If you have Sheet1, Sheet2, and Sheet3, and you use this formula on Sheet3, then it returns the value of Sheet2!A1. If the previous sheet is the first sheet of the workbook or it is not a worksheet, then the function returns a #Value error.
If you later copy this formula to a different sheet (say to Sheet 5), then it pulls up the value relative to its new location, which means it pulls up the value from Sheet4!A1.
You can also include a sheet name and the function will work just fine:
=PrevSheet(Sheet3!A5)
This version will always return Sheet2!A5 since Sheet2 is the previous sheet of Sheet3.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12141) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Relative Worksheet References.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Tired of hitting the F1 key by mistake and pulling up the Help system? Here are a couple of ways (one drastic and one not ...
Discover MoreIf your arrow keys and the Enter key aren't working as you expect them to, the problem could have any number of causes. ...
Discover MoreIt can be frustrating if you try to jump to the last cell in a worksheet, only to find out that you are taken to some ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-02-12 11:57:03
J. Woolley
The Tip's macro does not work as intended when more than one workbook is open; locate the following statement
PrevSheet = Sheets(i - 1).Range(rCell.Address)
and replace it with this statement
PrevSheet = rCell.Parent.Parent.Sheets(i - 1).Range(rCell.Address)
so Sheets(i - 1) applies to the workbook referenced by rCell.
My Excel Toolbox includes the following function to return a range on any worksheet in the workbook:
=RangeOnSheet(Target, [Position], [Absolute])
Target is the range to be returned on a worksheet determined by Position and Absolute (Sheet!Target). Position is the location of the required sheet in the workbook or 0 (default) for the formula's worksheet. If Absolute is FALSE (default), Position is relative to the formula's worksheet. If Absolute is TRUE, Position is the absolute location of the sheet in the workbook (base-1). Absolute is ignored if Position is 0 (the formula's worksheet). Chart sheets and worksheets plus hidden and very hidden sheets are included when determining location. The required sheet must be a worksheet; otherwise, Sheet!Target would be invalid.
The following examples are equivalent to the Tip's two examples:
=RangeOnSheet(A1, -1)
=RangeOnSheet(Sheet3!A5, (SHEET(Sheet3!A5) - 1), TRUE)
Only the cell range of Target applies; Target's worksheet does not apply.
Notice Excel's SHEET function returns the absolute position of a sheet; the SHEETS function returns the number of sheets (including hidden or very hidden).
This example will sum the cells in A1:A5 of the workbook's 3rd sheet:
=SUM(RangeOnSheet(A1:A5, 3, TRUE))
If Target is not contiguous it must be surrounded by parentheses; for example, (A1,A5). Notice Target might move if rows or columns are added, deleted, or moved on the formula's sheet or on Position's sheet; a named range will automatically adjust, but its scope must be either the workbook or the formula's sheet.
The result of RangeOnSheet can be verified as text using the following function available in My Excel Toolbox:
=RangeAddress(Range, [RowAbsolute], [ColumnAbsolute],
[ReferenceStyle], [External], [RelativeTo])
with Range replaced by RangeOnSheet(...). The optional arguments are described in Excel VBA's explanation of the Range.Address property.
My Excel Toolbox also includes the following function to return a sheet's name (text) based on its location (even if it is hidden or very hidden):
=NameOfSheet([Position], [Absolute])
Position and Absolute are the same as previously described.
See https://sites.google.com/view/MyExcelToolbox
2023-02-04 15:57:20
Tomek
If you omit the name of the sheet in the formula, i.e., use just =A1 then after copying the formula to another sheet will always reference to the sheet it is pasted on.
I am not aware of relative sheet addressing other than maybe using an =INDIRECT(...) function taking the referred-to sheet name from a cell on the same sheet.
-----------------------
On a positive note, if you rename a sheet, that change is carried to the formulas that reference that sheet.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments