Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. 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: Non-adjusting References in Formulas.

Non-adjusting References in Formulas

Written by Allen Wyatt (last updated July 23, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


1

Everybody knows you can enter a formula in Excel. (What would a spreadsheet be without formulas, after all?) If you use address references in a formula, those references are automatically updated if you insert or delete cells, rows, or columns and those changes affect the address reference in some way. Consider, for example, the following simple formula:

=IF(A7=B7,"YES","NO")

If you insert a cell above B7, then the formula is automatically adjusted by Excel so that it appears like this:

=IF(A7=B8,"YES","NO")

What if you don't want Excel to adjust the formula, however? You might try adding some dollar signs to the address, but this only affects addresses in formulas that are later copied; it doesn't affect the formula itself if you insert or delete cells that affect the formula.

The best way to make the formula references "non-adjusting" is to modify the formula itself to use different worksheet functions. For instance, you could use this formula in cell C7:

=IF(INDIRECT("A"&ROW(C7))=INDIRECT("B"&ROW(C7)),"YES","NO")

This formula constructs an address based on whatever cell the formula appears in. The ROW function returns the row number of the cell (C7 in this case, so the returned value is 7) and then the INDIRECT function is used to reference the constructed address, such as A7 and B7. If you insert (or delete) cells above A7 or B7, the reference in cell C7 is not disturbed, as it just blithely constructs a brand new address.

Another approach is to use the OFFSET function to construct a similar type of reference:

=IF(OFFSET($A$1,ROW()-1,0)=OFFSET($B$1,ROW()-1,0),"YES","NO")

This formula simply looks at where it is (in column C) and compares the values in the cells that are to its left. This formula is similarly undisturbed if you happen to insert or delete cells in either column A or B.

A final approach (and perhaps the slickest one) is to use named formulas. This is a feature of Excel's naming capabilities that is rarely used by most people. Follow these steps:

  1. Select cell C2.
  2. Display the Formulas tab of the ribbon.
  3. In the Defined Names group click the Define Name tool. Excel displays the New Name dialog box. (See Figure 1.)
  4. Figure 1. The New Name dialog box.

  5. In the Name box enter the name CompareMe. (You can use a different name, if you desire.)
  6. Erase whatever is in the Refers To box, replacing it with the following formula:
  7.      =IF(A2=B2,"YES","NO")
    
  8. Click OK.

At this point you've created your named formula. You can now use it in any cell in column C in this manner:

=CompareMe

It compares whatever is in the two cells to its left, just as your original formula was designed to do. Better still, the formula is not automatically adjusted as you insert or delete cells.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12348) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Non-adjusting References in Formulas.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Searching and Replacing Graphics

Got a bunch of graphics in a document that need replacing? (Perhaps you need to replace an old logo with a new one.) Word ...

Discover More

Removing a Macro from a Shortcut Key

Associate a macro with a shortcut key, and at some time you may want to break that association. (Perhaps so the shortcut ...

Discover More

Automatically Moving from Cell to Cell when Entering Data

As you enter data in a worksheet, you may want to have Excel automatically move from cell to cell based on the length of ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Pulling Initial Letters from a String

When working with names or a different series of words, you may need to pull the initial letters from each word in the ...

Discover More

Finding the Sum of a Sequential Integer Range

In mathematics, the sum of a range of sequential integers, starting with 1, is known as a triangular number or Gaussian ...

Discover More

Summing Filled Cells

If you have a column of data that contains color-coded cells, you may want a way to sum values in the column based on the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 1?

2022-07-26 14:50:02

J. Woolley

The Tip's CompareMe named formula with Workbook scope is clever but problematic because it is tied to the worksheet cell that was active when CompareMe was defined. For example, if Sheet1!C2 is active when CompareMe is defined as
=IF(A2=B2,"YES","NO")
the definition will immediately be changed to
=IF(Sheet1!A2=Sheet1!B2,"YES","NO")
Then if CompareMe is used in column C of Sheet2 its result will depend on the content of columns A and B on Sheet1 instead of Sheet2. This problem is mitigated when CompareMe is defined with Sheet1 scope; in this case, using CompareMe on Sheet2 will result in a #NAME? error.
It should also be noted that if Sheet1!C1 was active when CompareMe was defined as
=IF(A2=B2,"YES","NO")
then CompareMe will always compare cells that are in the next row (the row below).
For a better definition of CompareMe with Workbook scope which is not tied to the active worksheet cell, try this:
=IF(INDIRECT("R[0]C[-2]",FALSE)=INDIRECT("R[0]C[-1]",FALSE),"YES","NO")
In this case, CompareMe can be used on any sheet. (But be aware that INDIRECT is a Volatile function like OFFSET or NOW.)
It is worth noting that either version of CompareMe can also be used in column D to compare cells in columns B and C or in column E to compare cells in columns C and D; therefore, a better name might be AreTheLeftTwoCellsEqual.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.