IF, AND, OR and IFERROR in Excel
Make a spreadsheet decide: flag a bill as overdue, a budget line as over its limit, or a month as earning a bonus. IF, AND, OR and IFERROR, with the targets kept in their own cells.
- Level
- Beginner
- Reading time
- About 12 minutes
- Know this first
- A budget that sorts itself
By the end you will be able to:
- write an IF formula that returns different answers for true and false
- combine tests with AND and OR
- put a second IF inside the first for three outcomes and put the tests in the right order
- count the rows that meet a condition with COUNTIF
- use IFERROR narrowly and explain what it can hide
Letting the sheet decide
So far every formula has done arithmetic. An IF formula makes a choice. It tests whether something is true and returns one answer if it is and another if it is not. That is how a sheet marks a bill as overdue, a budget line as over its limit, or a month as having earned a bonus, without you looking at each row.
The examples reuse the shop's months from the lesson on core functions and the February budget from the lesson on SUMIFS.
IF: one test, two answers
IF(test, value_if_true, value_if_false). The test compares two things with one of the signs = equal, > greater than, < less than, >= at least, <= at most and <> not equal. Text answers go in quote marks, and number answers do not.
For example, =IF(B2>=14000, "Bonus", "No bonus") says that a month with sales of at least 14,000 earns a bonus. Better still, put the 14,000 in its own labelled cell and point at it with a locked reference such as $B$9, so the rule can be changed without editing every formula.
AND and OR: more than one test
AND(test1, test2) is true only when every test is true. OR(test1, test2) is true when at least one is. They go inside IF as its test. In the sheet the shop pays a bonus for a month when sales reach the sales target and the margin reaches the margin target (column E), and a looser version pays when either target is met (column F).
With targets of 14,000 and 26%, only April and May qualify under AND, because June had sales of 14,650 but a margin of only 25.6%. Under OR, June qualifies too. The summary cells count the bonus months with COUNTIF, a cousin of SUMIF that counts instead of adding: 2 months under AND and 3 under OR.
| Row | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| 1 | Month | Sales | Costs | Margin | Both targets | Either target |
| 2 | Jan | $12,400 | $9,300 | 25.0% | No bonus | No bonus |
| 3 | Feb | $13,150 | $9,800 | 25.5% | No bonus | No bonus |
| 4 | Mar | $12,800 | $9,650 | 24.6% | No bonus | No bonus |
| 5 | Apr | $14,200 | $10,400 | 26.8% | Bonus | Bonus |
| 6 | May | $15,100 | $11,000 | 27.2% | Bonus | Bonus |
| 7 | Jun | $14,650 | $10,900 | 25.6% | No bonus | Bonus |
| 8 | ||||||
| 9 | Sales target | $14,000 | ||||
| 10 | Margin target | 26% | ||||
| 11 | Bonus months, both | 2 | ||||
| 12 | Bonus months, either | 3 |
Change B10 to 25% and watch column E. The formulas do not change, because the rule lives in the targets, which is the point of keeping them in cells.
More than two answers: IF inside IF
When there are three outcomes, put a second IF in the “false” place of the first. Excel tests the first condition, and only if it fails does it move to the next. The budget status below reads: if the share used is above 100% then Over, otherwise if it is at least 90% then Near limit, otherwise OK.
Order matters. The strictest test goes first. If the 90% test came first, a line at 118% would be reported as Near limit. Nesting more than two or three levels becomes hard to read, and a lookup table, covered later in this module, is a better tool.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Category | Budget | Actual | Used | Status |
| 2 | Rent | $1,200 | $1,200 | 100% | Near limit |
| 3 | Groceries | $400 | $321 | 80% | OK |
| 4 | Transport | $100 | $52 | 52% | OK |
| 5 | Utilities | $90 | $88 | 98% | Near limit |
| 6 | Eating out | $150 | $177 | 118% | Over |
IFERROR: expecting the unexpected
Some formulas cannot work in some rows. Growth divides by last year's figure, and a product with no sales last year has a zero there. The plain formula gives #DIV/0!. IFERROR(formula, value_if_error) runs the formula and, if it produces any error, shows the second value instead.
Use it where you know why an error can appear and a clear replacement exists, as with the word new. Do not wrap every formula in IFERROR by habit. It hides real mistakes such as a wrong cell reference, and a hidden error is worse than a visible one.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Product | Last year | This year | Growth | With IFERROR |
| 2 | Tea | $4,200 | $4,650 | 10.7% | 10.7% |
| 3 | Coffee | $8,100 | $7,900 | −2.5% | −2.5% |
| 4 | Cake | $0 | $1,250 | #DIV/0! | new |
| 5 | Juice | $2,300 | $2,760 | 20.0% | 20.0% |
Try it yourself
The practice workbook holds the three sheets drawn above and a fourth with an invoice list. Mark each invoice Current, Late or Overdue and work out the late fee. The rules are written on the sheet, and the last sheet holds the answers.
Download the practice workbook (free .xlsx, no sign-up).
What this lesson leaves out
- Invented rules. The bonus targets, the budget limits and the late fee are made up to show how IF works. They are not rules to copy.
- No lookups yet. Long chains of IF are better replaced by a lookup table, which the next lesson covers.
- Text and numbers. IF can also test text, such as whether a cell says Paid, but the lesson keeps to numbers so the rules are easy to see.
- Newer functions. Recent Excel versions have IFS and SWITCH for several answers. They are left out because older versions and some other programs lack them.
Common mistakes
Quote marks in the wrong place
Text answers need quote marks. A number in quote marks is treated as text and will not add up.
Tests in the wrong order
With nested IF, put the strictest test first. Otherwise a later answer can never be reached.
Typing the target into the formula
A rule such as B2>=14000 hides its 14,000. Put targets in labelled cells so that anyone can see and change them.
Hiding errors
IFERROR can cover a wrong reference as easily as a harmless divide by zero. Use it narrowly and check the cells it covers.
Testing equality on calculated numbers
A margin worked out by division may differ from a typed 26% by a tiny fraction. Use at least or at most for targets, or round the number first.
Questions to answer
1. In the bonus sheet, which months earn a bonus when both targets must be met, and why not June?
Show the working
April and May. June had sales of 14,650, above 14,000, but a margin of 25.6%, below 26%, so under AND the second test fails.
2. In the bonus sheet, how many months earn a bonus when either target is enough?
Show the working
Three: April, May and June. Each meets at least one target, and January to March meet neither.
3. A budget line is at 100% used. What status does the nested IF give, and why?
Show the working
Near limit. The first test asks whether it is above 100%, and exactly 100% is not above. The second test, at least 90%, is true.
4. What does =IFERROR(C4/B4-1, "new") return when B4 is zero?
Show the working
The word new. Dividing by zero gives an error and IFERROR replaces it with the second value.
5. In the invoice list, which invoices are Overdue and what are the late fees?
Show the working
Corner Ltd (45 days) and Delta (31 days) are overdue. The fees are 2% of 2,400, which is $48.00, and 2% of 600, which is $12.00, a total of $60.00. The invoices at 0 and 12 days attract no fee.
Where this is used
- A finance team flags invoices that are overdue and charges a late fee.
- A manager marks budget lines that are near or over their limits.
- A sales team pays a commission only when targets are met.
- An analyst replaces errors in a ratio column with a clear label so that a report reads cleanly.
- A lender's checklist marks an application as complete only when every required item is present.
Key terms
- IF
- Returns one value when a test is true and another when it is false.
- AND
- True only when every test is true.
- OR
- True when at least one test is true.
- Nested IF
- An IF placed inside another IF to give more than two outcomes.
- COUNTIF
- Counts the cells that meet a condition.
- IFERROR
- Shows a chosen value in place of an error.
- Target cell
- A labelled cell holding a threshold that formulas point to.
Questions people ask
How many IFs can I nest?
Modern Excel allows 64 levels, but anything past two or three is hard to read. Use a lookup table instead.
Why does my IF return the formula as text?
The cell may be formatted as Text, or the formula may have been typed without a leading equals sign.
Can IF return a number or a calculation instead of a word?
Yes. The answers can be numbers, text, cell references or whole formulas, such as =IF(C4>30, B4*0.02, 0).
What does the double quote pair "" do?
It is an empty piece of text. =IF(A1="", "missing", A1) tests whether a cell is blank.
Sources and review
- Microsoft Support, IF function, for the arguments and for how many IF functions can be nested.
- Microsoft Support, Switch between relative, absolute, and mixed references, for how a reference changes when a formula is copied and the keys that add the dollar signs.
How the numbers were checked: every sheet drawn in this lesson was built as a real workbook, recalculated with LibreOffice, and drawn from the recalculated cells, so the figures are what a spreadsheet really computes. The worked answers were recomputed the same way. These are standard functions with the same definitions in Excel and Google Sheets. The practice file is the same workbook.