Written by Allen Wyatt (last updated August 29, 2022)
This tip applies to Excel 2007, 2010, 2013, and 2016
Marissa works for a lab where she uses Excel to prepare client reports showing the results of mold spore sampling. The first sample shown is always the baseline. All subsequent samples on the report need to be highlighted if they are 10x the baseline. Marissa has this figured out if the baseline is greater than 0, but if the baseline is 0, the samples should only be highlighted if they are 10 or more. (In other words, a baseline of 0 and 1 are treated exactly the same for comparison and highlighting purposes.)
Let's assume, for a moment, that the baseline value is in cell A2. Chances are good that Marissa has developed a formula for her conditional formatting rule which compares the sample values (let's say they start in cell A3) with the baseline value, multiplied by 10, like this:
=A3 >= ($A$2 * 10)
This returns True if the value in A3 is greater than or equal to the baseline value in cell A2. The only thing that needs to be done to this formula is to make sure that the baseline value being used is never less than 1. There are any number of adjustments that can be done to the formula. For instance, the following uses an IF statement to evaluate the baseline value. If it is equal to 0, then it returns a modified value of 10.
=A3 >= (IF($A$2 = 0, 10, $A$2 * 10))
You could also use the MAX function, if desired, which returns the largest value from a series of values:
=A3 >= (MAX($A$2,1) * 10)
Note that MAX will return either the baseline value in A2 or the value 1, whichever is greater. In other words, the baseline value will never be less than 1.
Finally, you could avoid worksheet functions in your formula altogether if you simply rely on how Excel handles Boolean logic. Consider this formula:
=A3 >= ($A$2 - ($A$2 = 0)) * 10
This works on the very simple principle that, if the baseline value (cell A2) contains 0, then ($A$2=0) returns the value -1 (the value Excel uses for True); if the baseline value is anything other than 0, then the test ($A$2=0) will return 0 (the value Excel uses for False). This value (-1 or 0) will then be subtracted from the baseline and multiplied by 10. In other words, if A2 contains zero, the formula will be tested against 0--1, which equals 1, and so the test will be performed as if A1 contained 1.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13447) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
The Conditional Formatting capabilities of Excel are powerful. This tip shows how you can use a simple approach to ...
Discover MoreIf you need to shade alternating rows in a data table, you'll want to examine how you can accomplish the task with ...
Discover MoreConditional formatting can be used to draw your attention to certain cells based on what is within those cells. This tip ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-08-20 12:42:57
Ken
It is NEVER good programming practice to rely on the internal representation of a value. Two major problems with this approach are: 1) it is opaque in that it is necessary to know how TRUE and FALSE are represented in order to understand the solution; 2) if the underlying platform/environment changes (like going from spreadsheet to macro, or porting to a different office suite), then a pretty insidious error can be introduced. Better to let TRUE be TRUE and not -1.
2016-06-21 13:23:53
David A. Gray
FWIW, I used the word TRUE in an abstract sense; TRUE is TRUE and FALSE is FALSE, and their underlying numerical values are irrelevant to working out the expression, which remains a problem in Boolean logic.
2016-06-20 17:38:45
Gary
Tony
I think True evaluates as 1 not minus 1, so the formula as you wrote works on that premise.
2016-06-20 15:45:50
David A. Gray
With regard to conditional formatting, I think the simplest way to think about the formatting formula is that the goal is to write an expression that evaluates to TRUE when you want the formatting to be applied.
2016-06-20 09:01:12
Willy Vanhaelen
@Tony
You are right.
The author of this tip overlooked the fact that, while in VBA TRUE equals -1, in a worksheet =TRUE equals 1 (positive).
Proof: enter =N(TRUE) or =TRUE*1 in a cell, in both cases the result is 1.
In my opinion =A3>=(MAX($A$2,1)*10) is easier to understand and even shorter.
2016-06-20 02:52:04
Tony
Isn't the formula
=A3 >= ($A$2 - ($A$2 = 0)) * 10
the wrong way round.
If $A$2 = 0 is true, the value is 1 (ie +1).
I'm using Excel 2010 and I need to use the formula
=A3 >= ($A$2 + ($A$2 = 0)) * 10
to get the correct result.
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 © 2023 Sharon Parq Associates, Inc.
Comments