Sub topic 8
Handling several outcomes with nested IF
This chapter assumes you can write a basic IF formula with a logical test, a value if true, and a value if false. It also assumes you know the comparison operators (=, <>, >, <, >=, <=) and can copy a formula down a column with the fill handle. If you have used AND and OR inside an IF, that helps, but you do not need it here.
A single IF gives two outcomes. Many real decisions have three or more: letter grades, shipping tiers, commission bands, or status labels. Nesting solves this by placing one IF inside another.
Why one IF is not enough
Suppose a score of 90 or more earns an "A", 80 to 89 earns a "B", 70 to 79 earns a "C", and anything lower is an "F". That is four outcomes. One IF can only separate two groups, so you need to ask several questions in order.
The idea is simple. The "value if false" argument of an IF can hold another IF. When the first test fails, Excel does not return a final answer. It moves on to a second question.
The nesting pattern
Here is the grading formula for a score in B2.
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))Read it from left to right as a series of questions:
- Is B2 at least 90? If yes, return "A" and stop.
- If not, is B2 at least 80? If yes, return "B" and stop.
- If not, is B2 at least 70? If yes, return "C" and stop.
- If not, return "F".
Each IF sits in the false position of the one before it. The last IF is the only one that returns two plain values. Three tests produce four outcomes. In general, N tests give N + 1 outcomes.
Why the order of the tests matters
Excel evaluates the tests from the outside in and stops at the first one that is true. The later tests never run for that row. This is why the formula tests the highest threshold first.
Consider what happens if you reverse the order.
=IF(B2>=70,"C",IF(B2>=80,"B",IF(B2>=90,"A","F")))A score of 95 passes the first test (95>=70), so the formula returns "C". The tests for 80 and 90 are never reached. Every score of 70 or more becomes a "C", and "A" and "B" can never appear. The formula has no error and looks reasonable, which makes this mistake easy to miss.
The rule: when you test with >=, go from the highest threshold to the lowest. When you test with < or <=, go from the lowest threshold to the highest.
Here is the same grading written with <, ordered from the bottom up.
=IF(B2<70,"F",IF(B2<80,"C",IF(B2<90,"B","A")))Both versions give the same grades. Choose one direction and use it consistently so the formula is easy to read later.
A worked example
Take a small table of scores.
| Row | A (Name) | B (Score) |
|---|---|---|
| 2 | Asha | 93 |
| 3 | Ben | 84 |
| 4 | Carla | 70 |
| 5 | Dev | 69.5 |
| 6 | Ema | 55 |
Enter the formula in C2 and fill it down to C6.
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))The results follow the order of the tests.
| Name | Score | Which test passed | Result |
|---|---|---|---|
| Asha | 93 | First (`>=90`) | A |
| Ben | 84 | Second (`>=80`) | B |
| Carla | 70 | Third (`>=70`) | C |
| Dev | 69.5 | None | F |
| Ema | 55 | None | F |
Carla's score of exactly 70 shows why >= matters. With > instead, 70 would fall through to "F". Dev's 69.5 shows another edge case: it is below 70, so it gets an "F" even though it rounds to 70. If you want rounding to count, apply it in the test, for example ROUND(B2,0)>=70.
Returning numbers and calculations from nested IF
The returned values do not have to be text. A common use is a tiered rate. Suppose sales in B2 earn a commission of 2% below 1000, 5% from 1000 to 4999, and 8% from 5000 up.
=B2*IF(B2>=5000,0.08,IF(B2>=1000,0.05,0.02))The nested IF returns only the rate, and the multiplication happens once outside. This is shorter and easier to change than repeating B2* in every branch. Note that this pays a flat rate on the whole amount, not a different rate on each slice of it. A sale of 6000 earns 8% of 6000, which is 480.
Putting the thresholds in cells
Typing 90, 80, and 70 inside a formula is called hard-coding. If the grading scale changes, you must edit every formula. A better approach is to store the thresholds in cells and refer to them with absolute references, so the references stay fixed when you copy the formula down.
Suppose E2 holds 90, E3 holds 80, and E4 holds 70.
=IF(B2>=$E$2,"A",IF(B2>=$E$3,"B",IF(B2>=$E$4,"C","F")))Now changing the value in E3 updates every grade. The dollar signs lock the column and row of each threshold. Without them, the references would shift as the formula is filled down, and you would compare scores against the wrong cells.
The limits of nesting
Nesting works, but it has costs.
- Readability. Each level adds a closing parenthesis at the end. With five or six levels, it is hard to see which
IFa value belongs to. - Maintenance. Inserting a new tier means placing a new
IFin the right spot and fixing the parentheses. - A hard limit. Modern Excel allows up to 64 nested
IFfunctions. Older versions (Excel 2003 and earlier) allow 7. A formula near either limit is a sign you need a different approach.
A practical guideline is to nest up to about three or four levels. Beyond that, consider IFS (covered in the next chapter) or a lookup table.
Mixing nesting with AND and OR
Sometimes a branch needs more than one condition. You can use AND or OR inside any of the nested tests. For example, a discount depends on both order size in B2 and customer type in C2.
=IF(AND(C2="Member",B2>=100),0.15,IF(OR(C2="Member",B2>=100),0.05,0))The first test requires both conditions, so members with large orders get 15%. If that fails, the second test needs only one of the two, so either a member or a large order gets 5%. Everyone else gets 0. The stricter test comes first. If you put the looser OR test first, it would catch the members with large orders too, and the 15% tier would never apply.
Common errors in nested IF
The mistakes below show up often. Each one is easy to diagnose once you know the pattern.
| Symptom | Likely cause | Fix |
|---|---|---|
| Excel shows "There's a problem with this formula" when you press Enter | Missing or extra parenthesis | Count opening and closing parentheses. You need one closing parenthesis for each `IF`. |
| Many rows show the same wrong result | Tests in the wrong order | Reorder so the most restrictive test comes first. |
| `#NAME?` | A text result without quotes, such as `A` instead of `"A"` | Put text results in double quotes. |
| `FALSE` appears in some cells | A missing final "value if false" | Add a final value so every path returns something. |
| Grades are off at the boundary | `>` used where `>=` was meant | Check each boundary value, such as exactly 90. |
| Formula works in the first row only | Thresholds typed as `E2`, not `$E$2` | Add dollar signs to the threshold references. |
The FALSE case deserves a note. If you leave out the last argument, as in =IF(B2>=90,"A",IF(B2>=80,"B")), any score below 80 returns the word FALSE. Excel is telling you that no branch handled the value. Always give the innermost IF both results.
Testing your formula
Before you trust a nested formula, test the values at and around each boundary. For the grading example, try 100, 90, 89.9, 80, 79.9, 70, 69.9, and 0. Each should give the grade you expect. You can also select part of the formula in the formula bar and press F9 to see what that part evaluates to. Press Esc afterward so you do not replace the formula with its value.
Exercise
A shipping fee depends on the order weight in B2. Weights under 1 kg cost 5, weights from 1 kg up to but not including 5 kg cost 8, and weights of 5 kg or more cost 12. Write a nested IF formula that returns the fee. Then say what the formula returns for a weight of exactly 5.
Show answer
One correct formula is =IF(B2>=5,12,IF(B2>=1,8,5)). It tests the highest threshold first. A weight of exactly 5 passes the first test (5>=5), so the formula returns 12. An equivalent version going from the bottom up is =IF(B2<1,5,IF(B2<5,8,12)), which also returns 12 for a weight of 5 because 5<1 and 5<5 are both false.
Exercise
A colleague wrote =IF(B2>=60,"Pass",IF(B2>=90,"Distinction","Fail")) and reports that no one ever gets "Distinction". Explain why and write a corrected formula.
Show answer
A score of 90 or more also satisfies B2>=60, so the first test returns "Pass" and the Distinction test is never reached. The tests are in the wrong order. The corrected formula is =IF(B2>=90,"Distinction",IF(B2>=60,"Pass","Fail")).