Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 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: Leaving a Cell Value Unchanged If a Condition Is False.

Leaving a Cell Value Unchanged If a Condition Is False

Written by Allen Wyatt (last updated August 8, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

While using the IF function, Vineet wants to retain the old value in the cell if the condition is false. In other words, the value in a cell where the IF function is used should change only if the condition being tested by the IF function is true. By default, however, the IF function makes the value 0 if the condition is False.

The IF function can take up to three parameters. The first parameter is the comparison that is to be made, the second parameter is what should be returned if the comparison is true, and the third is what should be returned if the comparison is false. It is possible to leave off the last parameter, but if you do then Excel will return the value 0 if the comparison is false. (This is what Vineet is seeing returned by his IF function usage.)

The obvious solution, then, is to make sure that you provide the IF function with something that should be returned when the comparison is false. For instance, let's say that your formula is in cell B1 and you are comparing something in cell A1. The formula you use may look like this:

=IF(A1<10,"under ten",B1)

Note that the words "under ten" are returned if the value in A1 is less than 10. If this condition is not met, then the value in B1 is returned. Since this formula is in cell B1, this means that the previous value in the cell is returned if the condition is false.

It also means that the formula contains a circular reference. For circular references to work OK you need to let Excel know that it is OK for them to occur in your worksheet. Follow these steps if you are using Excel 2010 or a later version:

  1. Display the File tab of the ribbon and then click Options. Excel displays the Excel Options dialog box.
  2. At the left side of the dialog box, choose Formulas. (See Figure 1.)
  3. Figure 1. The Formulas tab of the Excel Options dialog box.

  4. Make sure the Enable Iterative Calculation check box is selected.
  5. Click OK.

If you are using Excel 2007, choose Tools | Options | Calculation tab and make sure the Iteration check box is selected. Excel will now allow the circular reference without complaint.

If you don't want to allow a circular reference in your worksheet, then the only recourse is to create a macro that updates the value in cell B1 based upon any changes to cell A1:

Private Sub Workbook_SheetChange(ByVal Sh As Object, _
  ByVal Target As Range)

    ' See if the change is related to our cell
    If Not (Application.Intersect(Target, Range("A1")) _
      Is Nothing) Then
        If Range("A1") < 10 Then
            Range("B1") = "under ten"
        End If
    End If
End Sub

This simple macro, when added to the ThisWorkbook module, is executed every time there is a change in the workbook. If the value is cell A1 is changed (and only that cell), then the value is checked to see if it is less than 10. If it is, then the value in cell B1 is changed. If it isn't, then the value in cell B1 is left alone.

There is one "gotcha" that you need to keep in mind with any of the approaches discussed thus far, formula or macro. If the value in cell A1 is (let's say) 15, then cell B1 will contain what was there before, whatever it was. If you change the value in cell A1 to (let's say) 7, then B1 will change to "under ten." That's fine, but from that point on cell B1 will never appear to change. Why? Because if you then change cell A1 to a value greater than 10, cell B1 will contain (as just explained) what was there before. And, as you now understand, the value that was there before is the result of the previous true result, which was "under ten." Thus, true or false, the formula or macro from this point on displays the text "under ten."

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8264) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Leaving a Cell Value Unchanged If a Condition Is False.

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

Using the Address Book

Ways to use the address book when printing envelopes and labels.

Discover More

Understanding Conditional Formatting Conditions

Conditional formatting can be a great way to highlight specific information in your worksheets. This tip explains the ...

Discover More

Using AutoCorrect

The AutoCorrect feature in Excel is a great tool for quickly entering information. Here's an explanation of the feature ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Finding Unique Rows Based on Two Columns

Using the UNIQUE function you can derive unique values from a range of cells. By expanding the range of cells accessed by ...

Discover More

Calculating the Day of the Year

Need to know what day of the year a certain date is? You can figure it out easily using the formulas in this tip.

Discover More

Converting to Hexadecimal

Excel allows you to easily convert values from decimal to other numbering systems, such as hexadecimal. This tip explains ...

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 five minus 5?

2021-10-28 18:01:21

Option Crusader

Nice help. Very good!!


2021-01-31 15:31:33

Sean Pryor

This is so amazing. Updating a cell on a check has been a frustration forever! As a thank you, here is a fun script I wrote to pull the URL of the specific tab you are in. I use it to create a master directory that automatically pulls a URL column to click between tabs.

/*
From Sean Pryor 10/30/2020
This function will return the URL of the current tab.
*/

function Tab() {
var a = SpreadsheetApp.getActiveSpreadsheet();
var b = a.getActiveSheet().getSheetId();
var c = a.getUrl();
var d = c+"#gid="+b;
return d;
}


2020-09-10 15:03:06

Martin Farncombe

Now that, sir, is one of the best tips I have ever seen


2020-08-08 12:55:24

Lee McIntyre

Allen, you wrote:

There is one "gotcha" that you need to keep in mind with any of the approaches discussed thus far, formula or macro. If the value in cell A1 is (let's say) 15, then cell B1 will contain what was there before, whatever it was. If you change the value in cell A1 to (let's say) 7, then B1 will change to "under ten." That's fine, but from that point on cell B1 will never appear to change. Why? Because if you then change cell A1 to a value greater than 10, cell B1 will contain (as just explained) what was there before. And, as you now understand, the value that was there before is the result of the previous true result, which was "under ten." Thus, true or false, the formula or macro from this point on displays the text "under ten."

I believe there IS a solution that does not require a circular reference OR a macro.

The solution is elegant and simple (IMO!): Using your example, simply designate an intermediate cell in a hidden part of the worksheet to equal A1. Then make references in your formula refer to that hidden cell. In my example below, I have use Cell D1 as the intermediate cell. I can vary the value in A1, and each time, B1 reports properly.
{[fig}]


2020-08-08 12:02:14

Linda

I have been using a different formula if the result is false. I just substitute the "B1" in this example for "", which returns a blank cell. That eliminates having to turn off circular reference


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.