In this lesson

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

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.

A bonus rule: sales and margin per month, two targets, and two versions of the rule
RowABCDEF
1MonthSalesCostsMarginBoth targetsEither target
2Jan$12,400$9,30025.0%No bonusNo bonus
3Feb$13,150$9,80025.5%No bonusNo bonus
4Mar$12,800$9,65024.6%No bonusNo bonus
5Apr$14,200$10,40026.8%BonusBonus
6May$15,100$11,00027.2%BonusBonus
7Jun$14,650$10,90025.6%No bonusBonus
8
9Sales target$14,000
10Margin target26%
11Bonus months, both2
12Bonus months, either3
The rule is written once and copied down. The targets live in B9 and B10, so changing a target re-runs every month.

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.

A budget status column built with two IF functions, one inside the other
RowABCDE
1CategoryBudgetActualUsedStatus
2Rent$1,200$1,200100%Near limit
3Groceries$400$32180%OK
4Transport$100$5252%OK
5Utilities$90$8898%Near limit
6Eating out$150$177118%Over
Rent used exactly 100%, which is not above 100%, so it is flagged Near limit and not 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.

Growth by product, without and with IFERROR
RowABCDE
1ProductLast yearThis yearGrowthWith IFERROR
2Tea$4,200$4,65010.7%10.7%
3Coffee$8,100$7,900−2.5%−2.5%
4Cake$0$1,250#DIV/0!new
5Juice$2,300$2,76020.0%20.0%
Cake had no sales last year, so dividing by zero is not possible. The plain formula shows #DIV/0!. IFERROR replaces it with the word new.

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

  1. Microsoft Support, IF function, for the arguments and for how many IF functions can be nested.
  2. 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.