IF checks a condition and returns one result when it is true and another when it is false. =IF(B2>=50000, "Bonus", "No bonus") shows "Bonus" when B2 is at least 50,000 and "No bonus" otherwise. It is the most used function after SUM, and nearly every model uses it somewhere.
The three arguments
- logical_test is a comparison such as
B2>=50000, A2="Paid" or C2<>"", or any formula that returns TRUE or FALSE. A number is true unless it is zero. - value_if_true is returned when the test is true.
- value_if_false is returned when the test is false. If you leave it out completely, a false test returns the word
FALSE in the cell, which usually isn't what you want. Always give the third argument, even when it is "" for "show nothing".
Text must be in double quotes. Numbers and cell references aren't. The comparison is not case-sensitive, so A2="paid" is true for "Paid".
Combining tests
AND and OR let one IF test several conditions: =IF(AND(B2>=18, C2="Yes"), "Eligible", "Not eligible"). For more than two outcomes, you can nest IFs inside each other: =IF(B2>=85,"A",IF(B2>=70,"B","C")). Excel allows 64 levels, but anything past three becomes hard to read and easy to get wrong.
When to use something other than nested IF
- IFS lists the tests in order without nesting, and is clearer for several ranges.
- SWITCH compares one value with a list of exact matches, which suits codes such as "N", "S", "E" and "W".
- A lookup table, read with XLOOKUP or VLOOKUP, is better when the bands change often. You edit a table instead of a formula, and every formula that reads it stays correct.
Handling blanks and errors
An empty cell counts as zero in a number comparison and as empty text in a text comparison, so =IF(A2="", "", A2*2) is the usual way to leave a result blank when its input is blank. If the logical_test itself returns an error, IF returns that error. Wrap the calculation in IFERROR or IFNA to control it.
Things to avoid
Don't repeat the same calculation in both branches when only part of it changes. Move the common part out of the IF. And don't type constants such as tax rates or thresholds into the formula. Put them in cells and reference them, so a change is made once.
IF only evaluates the branch it needs, so it can protect a formula that would otherwise error: =IF(C2=0, 0, B2/C2) never divides when C2 is zero.