Field Guide

The IF function in Excel: nested IFs, IFS, and the mistakes that hide in them

Updated

IF is Excel’s decision function. =IF(logical_test, value_if_true, value_if_false) checks whether something is true, then returns one value if it is and a different value if it is not. The test is a comparison, C2>B2 say; when it holds, you get the second argument, and otherwise the third.

One IF makes one decision. Real workbooks stack several: discount tiers, grade bands, status flags. That stacking is where the mistakes creep in, the loud ones that error and the quiet ones that don’t.

How do I write an IF formula in Excel?

The first two arguments are required, the third is optional (though you should supply it, more on that below). Here a status column flags which budget lines ran over:

D2
fx
=IF(C2>B2, "Over budget", "OK")
A B C D
1 Category Budget Actual Status
2 Travel 500 620 Over budget
3 Software 300 280 OK
4 Training 400 400 OK
The test C2>B2 asks whether actual spend beat budget. 620 is more than 500, so D2 shows Over budget.

Fill the formula down and each row makes its own comparison. Notice row 4: actual spend of 400 against a budget of 400 reads OK, because 400>400 is false. That is correct here, but keep it in mind. Whether a boundary value lands in the true branch or the false branch is decided by that one > versus >= character, and it is where IF formulas go wrong most quietly.

The branches don’t have to be text. They can be numbers, cell references, or calculations. Using the same table, this returns the overspend amount instead of a label:

D2
fx
=IF(C2>B2, C2-B2, 0)

For the Travel row that is 620-500, so 120. For the rows at or under budget it returns 0.

How do multiple IF statements work in one formula?

To test more than one condition, you put a second IF where the value_if_false goes. Excel works through the conditions in order: it checks the first, and only when that fails does it move to the next. The first condition that passes wins, and the later branches no longer matter for the result. Nesting chains conditions in sequence like this; when you instead need several conditions to hold in a single test, all of them or any of them, wrap them in the AND, OR, and NOT functions rather than stacking more IFs.

Here is a realistic tiered discount: orders of 1,000 or more get 10%, orders of 500 or more get 5%, everything else gets nothing.

B2
fx
=IF(A2>=1000, 10%, IF(A2>=500, 5%, 0%))
A B
1 Order total Discount
2 1250 10%
3 800 5%
4 320 0%
A2 is 1250, so the first test passes and the result is 10%; the inner IF only matters when the first test fails. The Discount column is formatted to show percentages.

Because the first passing condition wins, the order of the conditions is part of the logic. This formula tests the biggest threshold first, which is why it works. Swap the tiers around and it stops working, without a single error:

=IF(A2>=500, 5%, IF(A2>=1000, 10%, 0%))
Every order of 500 or more passes this first test, so the 10% tier is unreachable. A 1,250 order gets 5%, and nothing flags it.

Microsoft’s own guidance on nesting is blunt: “While Excel will allow you to nest up to 64 different IF functions, it’s not at all advisable to do so.” And on why: “Multiple IF statements require a great deal of thought to build correctly and make sure that their logic can calculate correctly through each condition all the way to the end.”

Their worked example is a grading formula (the D2 here is the score column in Microsoft’s example sheet, not the discount table above):

fx
=IF(D2>89,"A",IF(D2>79,"B",IF(D2>69,"C",IF(D2>59,"D","F"))))

Four levels deep and it is already unpleasant: four conditions that must stay in descending order, four closing parentheses to balance, and a boundary at every step. Each tier you add makes the formula harder to read and gives a future editor one more place to break it. Past two or three levels, stop nesting. Use IFS, or replace the whole ladder with a lookup against a small tier table, which is what Microsoft’s pitfalls page also suggests; the XLOOKUP guide covers that pattern.

What is the IFS function and when should I use it?

IFS checks conditions in order and returns the value paired with the first one that is TRUE. Same logic as a nested IF, written flat:

fx
=IFS(logical_test1, value_if_true1, [logical_test2, value_if_true2],)

The discount ladder becomes:

B2
fx
=IFS(A2>=1000, 10%, A2>=500, 5%, TRUE, 0%)

Each condition sits next to its result, there is one closing parenthesis, and the order still matters exactly as before: biggest threshold first. Microsoft documents support for “up to 127 different conditions”, which is 127 more than any formula a colleague should have to read, but the headroom is there.

Two behaviors are worth knowing before you rely on it:

Why is my IF formula not working?

The failures split into errors you can see and results that are merely wrong. The visible ones first:

#NAME? means Excel doesn’t recognize something in the formula. The usual cause is a misspelled function name; Microsoft’s troubleshooting page adds two more, text values missing their quotation marks and a range reference missing its colon. For IFS there is an extra one: the workbook was opened in an Excel version that doesn’t have the function, so the function name itself is unknown there and every cell using it errors.

#VALUE! has two usual causes with IF: the syntax is malformed, easy to do four parentheses deep, or the formula references a cell that already contains an error. One sneaky variant is typing a threshold with a thousands separator, =IF(A2>1,000, ...), which splits the number at the comma. Microsoft’s warning here is hard to improve on: “The evaluation values in formulas don’t have commas. If you add them, the IF function will try to use them as arguments and Excel will yell at you.” IFS adds one more case: a condition that doesn’t resolve to TRUE or FALSE at all.

A 0 you didn’t ask for usually means an empty branch. Write =IF(A2>500, "Discount",) with that trailing comma and the false branch exists but holds nothing, so Excel shows 0. Microsoft lists exactly this among IF’s common problems. Give both branches an explicit value, even if one is just "".

If you’re staring at an error code this page hasn’t covered, Excel errors explained walks through every one of them and how to trace it back.

Can I combine IF with VLOOKUP or XLOOKUP?

Yes, and it’s a common pattern: look a value up, then decide something about it. Say A2:B6 holds product codes in column A and stock counts in column B, with a code to check in D2:

fx
=IF(VLOOKUP(D2, A2:B6, 2, FALSE)<10, "Reorder", "In stock")

The VLOOKUP fetches the stock count, the IF turns it into an instruction. The catch is what happens when the lookup misses: instead of one of your branches, the cell shows the lookup’s own error. The causes and fixes, including the IFNA versus IFERROR trade-off for handling an expected miss, are covered in why your VLOOKUP returns #N/A. If you’re on a current Excel, XLOOKUP sidesteps the wrapper entirely with its built-in not-found argument.

One boundary worth drawing: IF labels each row, one at a time. If what you actually want is to count or total the rows that meet a condition, that is a different family of functions, and COUNTIF and SUMIF do it in a single formula.

The IF mistake that doesn’t show up as an error

Every error above announces itself. The dangerous version of a broken IF doesn’t.

Take the discount ladder again, written with > instead of >=:

=IF(A2>1000, 10%, IF(A2>500, 5%, 0%))
An order of exactly 1,000 fails this test, drops to the next tier, and gets 5%. The top tier now starts just above 1,000, which is not what the policy said.

Nothing about that formula looks wrong. It calculates on every row, it returns a plausible percentage, and it is off by half right at the boundary. The same failure hides in a reordered condition, a tier threshold updated in one copy of the formula but not another, or a band edge that two people remembered differently. A wrong boundary returns a confident number, the number lands in a total, and the total ships.

So treat condition logic as something to check at its edges: test the exact threshold values themselves, and one value either side. And when a tier structure changes in a workbook other people rely on, compare the file before and after the edit. Seeing every changed formula and every changed result in one list is a much better safety net than hoping someone re-reads the parentheses.

Frequently asked questions

How do I write an IF statement in Excel?
An IF has three parts: =IF(logical_test, value_if_true, value_if_false). The test is a comparison that is either true or false, such as C2>B2. When it is true, Excel returns the second argument; otherwise it returns the third. For example, =IF(C2>B2, "Over budget", "OK") shows Over budget whenever the value in C2 is larger than the value in B2.
How many IF statements can you nest in Excel?
Excel allows up to 64 nested IF functions, and Microsoft itself says it is not at all advisable to go that far. Past two or three levels, the formula becomes hard to read and easy to break. Once conditions pile up, switch to IFS or to a lookup against a small tier table.
What is the difference between IF and IFS?
IF tests one condition and returns one of two values, so several conditions mean nesting IFs inside each other. IFS takes flat pairs of conditions and values, and returns the value for the first condition that is TRUE, up to 127 of them, without the pile of closing parentheses. IFS needs Excel 2019 or later, or Microsoft 365; IF works everywhere.
Why is my IF formula not working?
Check three things. A #NAME? error usually means the function name is misspelled, or the workbook uses IFS in an Excel version that does not have it. A #VALUE! error means the syntax is malformed or the formula references a cell that already contains an error. And a formula that calculates fine but gives a wrong answer usually has its conditions in the wrong order, or a boundary tested with > where you meant >=.
Why does my IF formula return 0?
A 0 usually means the branch the formula took is empty, for example a trailing comma with nothing after it, as in =IF(A2>500, "Discount",). Excel shows 0 for the missing argument. Give both branches an explicit value, even if one of them is just an empty string "".
When does IFS return #N/A?
IFS returns the #N/A error when none of its conditions evaluate to TRUE. To set a fallback instead, Microsoft's documented pattern is to enter TRUE as the final logical test and pair it with your default value: =IFS(A2>=1000, 10%, A2>=500, 5%, TRUE, 0%).

Catch the errors before they ship

Reviewing a change to a model? Compare the two versions in your browser and see every changed cell, formula, and value. It's free, and nothing leaves your computer.