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, and 2016. You can find a version of this tip for the older menu interface of Excel here: Relative Worksheet References.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Ever had your Excel status bar disappear unexpectedly? Here's some ideas on why this may be happening.
Discover MoreNeed to test your formulas? Then you need some testing data that you can use to see if the formulas function as you ...
Discover MoreWhat do you do if pasting information into a worksheet brings Excel to its knees? This tip looks at just a few ideas you ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-10-25 12:45:32
Lloyd Johnson
It's too bad Microsoft can't just incorporate this as a function into the program like Quattro Pro did a long time ago instead of us having to write macros.
2017-01-03 15:22:29
Steve Bondy
I tried this but kept getting a #Name error. For anyone else that runs into that, you MUST put the macro in a user module. In the VB editor click on the insert menu, then click on Module.
Paste the VB code into the newly created module, then it all works properly. You can't put the code in the "This Workbook" module, or in the module for one of the individual sheets. It must be in a user module.
See the first two steps of this article for further reference:
http://www.dummies.com/software/microsoft-office/excel/how-to-create-custom-excel-functions/
2016-07-15 11:40:49
Ashley
I am trying to set this up and I keep getting #Value error. I set this up through the VBA which is what I assume you mean when you say create a macro. That is the only way it would work. PreSheet comes up but once I enter the cell vaule for the previous page I get the error. Please help, thanks
2015-11-01 00:44:01
CKChan
Thanks, it's really useful.
2015-10-05 09:22:54
TimZ
Thanks, I think this will will come in handy. In the past, I've relied on named references or used the INDIRECT function to simplify relative references across sheets, but both those options come with some overhead.
2015-08-26 02:45:42
Rajeswar Rao NLV
I have tried this exact macro and it worked for me. It has saved me from enormous work. PrevSheet function is working fine in Macro enable workbook of office 2007 and 2010.
Thank You ExcelTips.
2015-08-04 08:09:04
Blak11
How would I modify this macro to skip hidden sheets..?
2015-02-17 11:46:25
Jeremy
I have tried this exact macro multiple times. I keep getting the return value of "#VALUE!". I know the macro is correct I have copied it from this website. Any ideas of what I could be doing wrong?
2013-03-20 11:20:39
I tested the function and it actually does depend on the order of the tabs along the bottom. The function looks for the sheet previous to the sheet the function is used in. You will get a #VALUE! error if you use this function on the first sheet/tab of the series.
2013-03-19 14:12:53
Dennis Costello
In the above, "which worksheet is before it" depends of course not on the order of the tabs at the bottom of the window or the names of the worksheets, but their index numbers, which roughly translates to the order in which they were created. Sheet1, Sheet2, and Sheet3 are a special case in that when a new workbook is created, they have indices 1, 2, and 3.
2012-09-24 19:16:33
Glenn
Nice one, thanks Allen
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 © 2021 Sharon Parq Associates, Inc.
Comments