Sub topic 10
IFERROR: providing a fallback when a formula fails
This chapter assumes you can enter and edit formulas in Google Sheets, use cell references and ranges such as B2 and A:A, apply the comparison operators, and use the basic IF function. It also assumes you have met the common error values in Sheets, such as #N/A and #DIV/0!. The chapter covers how IFERROR works, where it fits in a formula, and how to use it without hiding real problems.
What IFERROR does
When a formula cannot produce a result, Sheets shows an error value in the cell. That is useful while you build a formula. In a finished report, error values look broken, and they spread. Any formula that refers to a cell containing an error usually returns an error too.
IFERROR wraps a formula and replaces any error it produces with a value you choose. Its syntax is:
=IFERROR(value, [value_if_error])valueis the formula or expression to evaluate.value_if_erroris what to return ifvalueproduces an error. It is optional. If you leave it out, Sheets returns an empty cell (a blank) instead of the error.
If value does not produce an error, IFERROR returns it unchanged. The fallback is only used when something goes wrong.
A first example: division by zero
Suppose column C holds the amount spent and column D holds the number of orders. You want the average spend per order in column E:
=C2/D2If D2 is zero, the result is #DIV/0!. Wrapping the division in IFERROR returns a fallback of 0 instead:
=IFERROR(C2/D2,0)| C2 | D2 | `=C2/D2` | `=IFERROR(C2/D2,0)` |
|---|---|---|---|
| 100 | 4 | 25 | 25 |
| 100 | 0 | #DIV/0! | 0 |
| 0 | 5 | 0 | 0 |
The last two rows show a weakness. A zero in the result can mean "the average is really zero" or "there was nothing to divide by." The fallback hides that difference. Later in this chapter you will see why the choice of fallback matters.
A second example: a lookup that finds nothing
VLOOKUP returns #N/A when it cannot find the search key. Here is a price lookup against a sheet named Prices:
=IFERROR(VLOOKUP(A2,Prices!A:B,2,FALSE),"Not found")If A2 appears in column A of Prices, the formula returns the matching price from column B. If it does not, the cell shows Not found.
The fallback is text here, and the normal result is a number. A column that mixes numbers and text is fine for display. It can cause trouble if you later add up the column or use it in another calculation. SUM ignores text, but the arithmetic operator + does not. =E2+1 where E2 contains Not found gives #VALUE!.
How IFERROR evaluates
IFERROR calculates value first. If the result is an error of any kind, it returns value_if_error. Otherwise it returns the result. Three details follow from this.
- It catches every error type. That includes
#N/A,#DIV/0!,#VALUE!,#REF!,#NAME?, and#NUM!. You cannot tellIFERRORto catch only some of them. For that you needIFNA, which the next chapter covers. - It works on the final result of the expression, not on parts of it. If you wrap a long formula, an error anywhere inside it that reaches the final result triggers the fallback. An error in a branch that is never evaluated does not.
- The fallback can be any expression. It can be a number, text, an empty string
"", a cell reference, or another formula.
Choosing a fallback value
The fallback decides how the rest of your sheet behaves. These are the common choices:
| Fallback | Example | Good for | Watch out for |
|---|---|---|---|
| `0` | `IFERROR(C2/D2,0)` | Numeric columns that feed sums or charts | Looks like a real zero |
| `""` (empty text) | `IFERROR(C2/D2,"")` | Clean-looking reports | The cell is text, not blank. `=E2+1` gives `#VALUE!` |
| A message | `IFERROR(...,"Not found")` | Telling a reader what happened | Mixes text into a numeric column |
| Another formula | `IFERROR(VLOOKUP(...),VLOOKUP(...))` | Trying a second source | Second formula can also fail |
| No second argument | `IFERROR(C2/D2)` | A quick blank | Easy to miss when reading the formula |
An empty string "" looks blank but is a text value of zero length. COUNTA counts it as non-empty, and ISBLANK returns FALSE for it. If you want a cell that other formulas treat as truly empty, IFERROR cannot do that. Omitting the second argument returns an empty value that behaves like a blank cell in most formulas, but it hides the intent from anyone reading the formula.
Using IFERROR with other functions
With IFS
IFS returns #N/A when no condition is true. A blank cell, or a score entered as text, can also cause errors or fall through every test. Wrapping IFS in IFERROR gives the reader a clear message:
=IFERROR(IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C"),"Check score")Here there is no TRUE fallback inside IFS, so any score below 70 matches no condition, and IFERROR replaces the resulting #N/A with Check score. That may or may not be what you want. A score of 55 is a valid score that deserves a grade, not a request to check the data. If you add TRUE,"F" inside IFS, then low scores get an F and IFERROR is left to catch real problems, such as B2 containing the text absent.
Compare these two designs:
=IFERROR(IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F"),"Check score")With the TRUE fallback, the logic covers every number. The outer IFERROR then handles only cases the logic cannot, such as an error value already sitting in B2. This is the better division of labor: IFS handles expected cases, and IFERROR handles the unexpected ones.
With SWITCH
SWITCH returns #N/A when nothing matches and no default is given. You usually do not need IFERROR for that, because the default value argument does the same job:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")Use IFERROR around SWITCH only when the input expression itself might fail, for example:
=IFERROR(SWITCH(WEEKDAY(A2),1,"Sunday",7,"Saturday","Weekday"),"Invalid date")If A2 holds text that is not a date, WEEKDAY returns #VALUE!. SWITCH passes that error through, and IFERROR turns it into Invalid date. The SWITCH default handles unmatched days, and IFERROR handles bad input. Each tool covers a different failure.
Applying IFERROR to a whole column
You can wrap an array formula in IFERROR to handle every row at once. For example, with ARRAYFORMULA:
=ARRAYFORMULA(IFERROR(C2:C/D2:D,0))This divides each row of column C by the matching row in D and returns 0 for any row that errors. Note that empty rows below your data also produce #DIV/0! (blank divided by blank), so they come back as 0 too. If you want those rows to stay empty, add a condition:
=ARRAYFORMULA(IF(D2:D="","",IFERROR(C2:C/D2:D,0)))The outer IF leaves empty rows blank, and the inner IFERROR handles true division errors.
When IFERROR hides real problems
IFERROR is convenient, and that is the danger. Because it catches every error, it can bury mistakes you would want to see. Consider this formula:
=IFERROR(VLOOKUP(A2,Prices!A:B,2,FALSE),"Not found")You expect "Not found" when A2 is missing from the price list. Now imagine someone deletes the Prices sheet, or a column is removed so the range no longer holds two columns. VLOOKUP returns #REF!. IFERROR converts it to Not found, and every row reports that the product is missing. The message points you at the data when the real fault is in the formula.
A misspelled function name behaves the same way. =IFERROR(VLOKUP(A2,Prices!A:B,2,FALSE),"Not found") produces #NAME? inside, and you see only Not found in every row. Nothing signals that the formula is broken.
Ways to reduce the risk:
- Use
IFNAwhen only#N/Ais expected. For lookups,IFNAlets#REF!,#NAME?, and other errors show through. The next chapter covers it in detail. - Test the formula without the wrapper first. Build and check the inner formula, and add
IFERRORlast. - Choose a fallback that stands out. A message such as
Not foundis easier to notice than0. A numeric0quietly flows into totals and charts. - Count the fallbacks. If you use a distinctive message,
=COUNTIF(E2:E,"Not found")tells you how many rows fell back. A sudden jump means something changed. - Keep the wrapped formula short. The larger the formula inside
IFERROR, the more kinds of errors it can hide.
Common mistakes
- Using
0as the fallback for averages and rates. A zero pulls down averages built from that column. A blank or a message is often more honest. - Treating
""as blank. It is text. Arithmetic on it fails, andISBLANKsaysFALSE. - Nesting
IFERRORto chain lookups without a final message.=IFERROR(VLOOKUP(...),VLOOKUP(...))returns an error if the second lookup also fails, because only the first lookup is protected. Wrap the whole thing, or end with a text fallback. - Wrapping every formula by habit. Errors are information. Use
IFERRORwhere you have a specific reason to expect a failure and a sensible response to it. - Forgetting the argument order. The formula to test comes first, the fallback second. Reversing them returns the fallback every time, or an error when the "fallback" is itself the problem.
Summary of behavior
| Situation | Result of `IFERROR(x, "fallback")` |
|---|---|
| `x` evaluates to a number | The number |
| `x` evaluates to text | The text |
| `x` is an empty cell reference | The empty cell's value (0 or blank, depending on use) |
| `x` is `#DIV/0!`, `#N/A`, `#VALUE!`, `#REF!`, `#NAME?`, or `#NUM!` | `"fallback"` |
| `x` is a text value that looks like an error, such as `"#N/A"` typed as a string | The text, since it is not a real error |
Exercise
A sheet has product codes in A2:A and a price list in Prices!A:B. You write =IFERROR(VLOOKUP(A2,Prices!A:B,2,FALSE),"Not found"). A colleague deletes column B of the Prices sheet, and every row now shows Not found. Explain why, and rewrite the formula so the real problem is visible.
Show answer
Deleting column B makes the lookup range invalid, so VLOOKUP returns #REF! (the column index 2 is outside the range). IFERROR catches every error type, so it replaces #REF! with "Not found", which makes the data look incomplete when the formula is broken. Use IFNA instead: =IFNA(VLOOKUP(A2,Prices!A:B,2,FALSE),"Not found"). IFNA replaces only #N/A, the error that VLOOKUP returns when a code is truly missing, so #REF! now shows in the cell and exposes the broken range.
Exercise
You have =IFERROR(C2/D2,"") in E2 and =E2*2 in F2. When D2 is 0, what does F2 show, and how could you change E2 so F2 still works?
Show answer
E2 returns an empty string, which is text, so =E2*2 gives #VALUE!. Change the fallback in E2 to 0 (=IFERROR(C2/D2,0)) so that F2 gets a number, or make F2 handle the text, for example =IF(E2="","",E2*2). Choose 0 only if a zero result is acceptable for your report, since it cannot be told apart from a real zero.