Sub topic 8
Nesting IF for more than two outcomes
This chapter assumes you can write a basic IF(test, value_if_true, value_if_false) formula and know the comparison operators >=, <, = and <>. It also assumes you can copy a formula down a column with relative references. Here you will build formulas that choose among three or more results by placing one IF inside another.
Why one IF is not enough
A single IF has exactly two exits: one for TRUE and one for FALSE. Many real decisions have more. A score becomes a letter grade, an invoice becomes "Current", "Late" or "Overdue", and an order total earns one of several discount rates.
Nesting solves this by putting a second IF where the first IF's value-if-false would go. If the first test fails, the second IF takes over and asks its own question. You can repeat this as many times as you need.
Reading a nested IF
Here is the grading formula from the plan:
=IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "F")))Read it from left to right as a chain of questions:
- Is B2 at least 90? If yes, return "A" and stop.
- Otherwise, is B2 at least 80? If yes, return "B" and stop.
- Otherwise, is B2 at least 70? If yes, return "C" and stop.
- Otherwise, return "F".
Each inner IF sits in the third argument of the IF around it. The outer IF never evaluates its inner IF unless its own test is FALSE.
Notice the shape. With three tests there are four possible results. In general, n tests give n + 1 outcomes, because the last IF has both a TRUE and a FALSE exit.
Order matters: the first TRUE wins
A nested IF stops at the first test that is TRUE. The later tests are never looked at. This means the order of your tests is part of the logic.
Consider the same grades with the tests in the wrong order:
=IF(B2>=70, "C", IF(B2>=80, "B", IF(B2>=90, "A", "F")))A score of 95 passes B2>=70 immediately, so the formula returns "C". The tests for "B" and "A" are unreachable. Nothing is flagged as an error. The formula just gives wrong answers.
The rule for numeric ranges is to order your tests so each one is stricter than the ones after it. Two safe patterns work:
- Highest threshold first, using
>=. This is the pattern in the grading example. Each test only needs a lower bound because earlier tests already removed the higher values. - Lowest threshold first, using
<. For example,IF(B2<50, "F", IF(B2<70, "C", ...)). Each test needs only an upper bound.
Pick one direction and keep it for the whole formula. Mixing >= and < in the same chain is a common source of off-by-one boundary mistakes.
Boundaries and inclusive operators
The choice between > and >= decides which group an exact boundary value falls into. In the grading formula, a score of exactly 90 returns "A" because the test is >=. If you wrote B2>90, a 90 would drop to "B".
Before you finish a nested IF, test the values on each boundary. For the grading formula, try 90, 89.99, 80, 79.99, 70 and 69.99. Boundary checks catch more errors than testing typical values.
Text results versus text tests
Nested IF works on text as well as numbers. Suppose column A holds a status, and you want a different action for each one:
=IF(A2="Paid", "Done", IF(A2="Pending", "Wait", IF(A2="Overdue", "Chase", "Check status")))The final value-if-false is a catch-all. Here it returns "Check status" for anything unexpected, such as a typo like "Pad". This is a useful habit: end your chain with a fallback that makes unexpected data visible, rather than one that quietly looks like a normal result.
Remember that text comparison with = is not case-sensitive, so "paid", "PAID" and "Paid" all match. It is sensitive to extra spaces, though. A cell containing "Paid " with a trailing space does not equal "Paid" and falls through to the fallback.
Always supply the last value-if-false
If you leave out the final value-if-false, the last IF returns FALSE when its test fails:
=IF(B2>=90, "A", IF(B2>=80, "B"))A score of 75 passes neither test, so the cell shows FALSE. A stray FALSE in a column of letters is easy to miss. Write the fallback every time, even if it is an empty string "" to show nothing.
Handling blank cells in a chain
An empty cell compared with a number behaves as if it were 0. In the grading formula, a blank B2 fails every test and returns "F". For rows that have not been filled in yet, that is misleading.
Add a blank check as the outermost IF, so it runs first:
=IF(B2="", "", IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "F"))))Now a blank row stays blank, and the grading chain runs only when a score exists. This pattern, with the guard on the outside and the real logic inside, works for any nested formula.
Combining nesting with AND and OR
Each test in a chain can itself be a compound condition. Suppose a bonus depends on both sales and tenure:
=IF(AND(B2>=100000, C2>=5), "Gold", IF(OR(B2>=100000, C2>=5), "Silver", "None"))Here B2 is sales and C2 is years of service. The first test needs both conditions. If it fails, the second test needs only one of them. Because the strict test comes first, a person who meets both conditions is never reported as "Silver".
When ranges are bounded on both sides
If you order the tests correctly, you rarely need to write both bounds. Sometimes the order is not natural, for example when each branch tests a different column. In that case, state each range fully with AND:
=IF(AND(B2>=0, B2<50), "Low", IF(AND(B2>=50, B2<100), "Medium", IF(B2>=100, "High", "Out of range")))This version is longer, but each branch can be understood on its own. It also handles bad data: a negative number or text value reaches the final fallback, "Out of range", instead of being classified as something it is not.
A text value in B2, such as "abc", is treated as larger than any number in a comparison. In the first chain, it would therefore pass B2>=90 and return "A". The bounded version above avoids this, because B2<50 and B2<100 are FALSE for text and B2>=100 is TRUE. If your data may contain text, add a guard such as ISNUMBER(B2) as the outermost test.
=IF(ISNUMBER(B2), IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "F"))), "Not a number")Keeping parentheses balanced
Every IF opens one parenthesis and needs one closing parenthesis. In a nested chain, the closing parentheses pile up at the end. Count one ) for each IF( you typed.
The grading formula has three IF( openings, so it ends in three closing parentheses: "F"))). If you add a fourth level, you need a fourth.
Some habits help:
- Build the formula from the inside out. Write and test the last IF alone, then wrap it in the one before.
- Use the formula bar, which you can expand by dragging its bottom edge. Google Sheets colors matching parentheses, and when you click next to one it highlights its partner.
- Press
Alt+Enteron Windows orOption+Enteron Mac inside the formula bar to add a line break. Sheets ignores line breaks in formulas, so you can put each IF on its own line.
=IF(B2>=90, "A",
IF(B2>=80, "B",
IF(B2>=70, "C",
"F")))The formula above works the same as the one-line version. The layout shows which test belongs to which level.
Copying a nested formula
The fill-down rules from earlier still apply. Relative references like B2 shift as you copy the formula down. Thresholds that you may want to change later are better stored in cells and referenced absolutely:
=IF(B2>=$F$1, "A", IF(B2>=$F$2, "B", IF(B2>=$F$3, "C", "F")))Now the cutoffs 90, 80 and 70 live in F1:F3. Changing a cutoff updates every row, and nobody has to edit the formula. Without the $ signs, F1 would shift to F2, F3 and so on as you copy down, and the thresholds would be wrong.
Where nesting stops being a good idea
Nested IF is fine for three or four outcomes. Beyond that, the formula becomes hard to read and easy to break, because a change to one level means counting parentheses again. Signs that you have outgrown it:
- You need to scroll sideways to read the formula.
- You are editing the same formula in many places when a rule changes.
- Colleagues ask you what it does.
For flatter formulas with the same logic, the next chapter introduces IFS. Many chains of cutoffs are also better expressed as a small lookup table, but that is beyond simple IF formulas. The skill you build here still matters, because IFS and nested IF follow the same rule: the first TRUE wins.
Common mistakes with nested IF
| Symptom | Likely cause | Fix |
|---|---|---|
| Everyone gets the lowest grade | Tests ordered lowest threshold first with `>=` | Put the highest threshold first |
| Cell shows FALSE | Final value-if-false was omitted | Add a fallback value as the last argument |
| "We found an error in the formula" message | Missing or extra closing parenthesis | Count one `)` per `IF(` |
| Text outcome shows as a name error | Result text not in quotes | Wrap results in straight double quotes |
| Boundary value in the wrong group | `>` used where `>=` was meant | Test the exact boundary values |
| Blank rows show "F" | Blank cell is treated as 0 | Add `IF(B2="", "", ...)` as the outer test |
Exercise
Column B holds a delivery time in days. Write a formula for C2 that returns "Fast" for 2 days or fewer, "Normal" for 3 to 5 days, "Slow" for 6 or more days, and stays empty when B2 is blank. Give the formula and say what happens for a delivery time of exactly 5.
Show answer
=IF(B2="", "", IF(B2<=2, "Fast", IF(B2<=5, "Normal", "Slow"))). The blank check runs first so empty rows show nothing. The tests go from the lowest threshold up and use <= throughout. A value of exactly 5 passes B2<=5 but fails B2<=2, so it returns "Normal".
Exercise
A colleague wrote =IF(B2>=50, "Pass", IF(B2>=75, "Merit", IF(B2>=90, "Distinction", "Fail"))). A score of 95 returns "Pass". Why, and how do you fix it?
Show answer
The first test, B2>=50, is TRUE for 95, so the formula stops there and never reaches the Merit or Distinction tests. Reorder from the highest threshold: =IF(B2>=90, "Distinction", IF(B2>=75, "Merit", IF(B2>=50, "Pass", "Fail"))).