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.
Written by Allen Wyatt (last updated April 2, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
Figure 1. The Formulas tab of the Excel Options dialog box.
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:
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
If you need to know the number of columns in a particular range, you can use the COLUMNS worksheet function. This tip ...
Discover MoreProgrammers know that a staple of any language is the ability to create conditional statements. Excel understands this, ...
Discover MoreThe COMBIN function is used to determine the number of combinations that can be made from a group of elements. This tip ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-08-13 14:10:40
J. Woolley
@Dexraj
Sorry for the mess in my previous comment below. That is what happens if you submit a comment that is "suspected as spam" and you click to "have your comment reviewed." Everything becomes compressed into a single paragraph. I'll try again with the following:
Did you use a Check Box from the Form Controls or the ActiveX Controls? Did you link it to cell U5?
See https://excelribbon.tips.net/T008392_Using_Check_Boxes.html
Assuming you used a Check Box from the Form Controls and linked it to cell U5, do this:
1. Press Alt+F11 to open the Visual Basic Editor (VBE).
2. From the VBE menu, pick Insert > Module.
3. Add this VBA to the new module:
Public Sub CheckBoxEvent()
If Range("U5").Value Then Range("D5").Value = Range("S5").Value
End Sub
4. Delete your previous VBA.
5. Right-click the Check Box and pick Assign Macro..., then pick CheckBoxEvent and click OK.
6. Click outside the Check Box to unselect it.
Now when you toggle the Check Box cell U5 from FALSE to TRUE, the value of cell S5 will be copied to cell D5. If you change S5 or toggle the Check Box cell U5 from TRUE to FALSE, there is no change to D5. If you change D5, that change will persist until you toggle the Check Box cell U5 from FALSE to TRUE.
2023-08-13 12:22:06
J. Woolley
@Dexraj Did you use a Check Box from the Form Controls or the ActiveX Controls? Did you link it to cell U5? See https://excelribbon.tips.net/T008392_Using_Check_Boxes.html Assuming you used a Check Box from the Form Controls and linked it to cell U5, do this: 1. Press Alt+F11 to open the Visual Basic Editor (VBE). 2. From the VBE menu, pick Insert > Module. 3. Add this VBA to the new module: Public Sub CheckBoxEvent() If Range("U5").Value Then Range("D5").Value = Range("S5").Value End Sub 4. Delete your previous VBA. 5. Right-click the Check Box and pick Assign Macro..., then pick CheckBoxEvent and click OK. 6. Click outside the Check Box to unselect it. Now when you toggle the Check Box cell U5 from FALSE to TRUE, the value of cell S5 will be copied to cell D5. If you change S5 or toggle the Check Box cell U5 from TRUE to FALSE, there is no change to D5. If you change D5, that change will persist until you toggle the Check Box cell U5 from FALSE to TRUE.
2023-08-12 11:04:40
Dexraj
Thank you for your post. I am trying to update the date in a cell (in cell D5) to the new date (that is in cell S5) when the this year's work is done (that is once a check Box in cell U5 is checked or is TRUE), and tried to use your code that didn't work, then modified code as follows, and still not updating to new date, once check box is checked. The need to be updated once the checkbox is checked, otherwise remains unchanged.
I tried following 3 codes, none is updating the date in column D(cell D5)
'Private Sub Worksheet_SheetChange(ByVal Sh As Object, _
' ByVal Target As Range)
'
'If (U5.Value = True) Then D5.Value = S5.Value Else D5.Value
'
'
' ' See if the change is related to our cell
' If (Application.Intersect(Target, Range("U5")) _
' Is False) Then
' If Range("U5") = True Then
' Range("D5") = Range("S5")
' End If
' End If
'End Sub
Private Sub Sheet1_Change(ByVal Target As Range)
If Sheet1.Range("U5").Value = True Then 'cell U5 is a checkbox
Sheet1.Range("D5").Value = Sheet1.Range("S5").Value ' cell D5 has this years date, and cell S5 has new date a year later
Else
Sheet1.Range("D5").Value
End If
End Sub
Would appreciate if you could please identify the issue, and let me know how to fix it.
Thanks
2021-10-28 18:01:21
Option Crusader
Nice help. Very good!!
2021-01-31 15:31:33
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
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