Sub topic 3
Catching errors with IFERROR
This chapter assumes you can enter and copy formulas, use relative and absolute references, and write a basic IF and VLOOKUP. It covers how IFERROR works, the replacement values it can return, and the mistakes that come up most often.
What IFERROR does
IFERROR runs a formula. If the formula returns any error value, IFERROR returns a fallback you choose. If the formula works, IFERROR returns its normal result.
The syntax is:
=IFERROR(value, value_if_error)valueis the formula or expression to test.value_if_erroris what you get back whenvalueproduces an error.
The simplest case is a division that can fail:
=IFERROR(A2/B2, 0)If B2 is 0, A2/B2 would show #DIV/0!. IFERROR returns 0 instead. If B2 is 5, you get the quotient as usual.
The decision flow
IFERROR evaluates value once and then picks one of two paths.
Which errors it catches
IFERROR catches all seven error types: #DIV/0!, #N/A, #VALUE!, #REF!, #NAME?, #NUM! and #NULL!. It does not tell you which one occurred. That is convenient, and it is also the main risk, which is covered below.
Choosing the fallback
The fallback can be a number, text, or an empty string.
=IFERROR(A2/B2, "")The empty string "" looks blank, but the cell is not empty. It holds text of zero length. This matters later, because ISBLANK returns FALSE for that cell, and some formulas treat it differently from a true blank.
A message works well when a person reads the sheet:
=IFERROR(A2/B2, "Check divisor")| Fallback | Looks like | Typical use | Watch out for |
|---|---|---|---|
| `0` | The number 0 | Values that feed totals | A real zero and a failure look the same |
| `""` | A blank cell | Reports for reading | Text in a numeric column |
| `"Check input"` | A message | Sheets reviewed by people | Text breaks later arithmetic |
Effects on dependent formulas
The fallback becomes the cell's value, so other formulas see it. SUM ignores text, so a text fallback is skipped in a total. But =C2*2 on a cell containing "" returns #VALUE!, because text cannot be multiplied. A fallback of 0 keeps arithmetic working, but it can also drag down an AVERAGE, since the 0 is counted as a real value.
Using IFERROR with a lookup
IFERROR is often wrapped around a lookup to handle a missing key:
=IFERROR(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found")This works, but it catches more than a missing key. If the table range is deleted and the formula turns into #REF!, or you mistype the function name and get #NAME?, the cell still says "Not found". The next chapter covers IFNA, which is narrower and safer for lookups.
Common errors
- Wrapping everything by habit. A broken formula that shows a polite message is harder to find than one showing
#REF!. Add IFERROR after the formula works, not while you are building it. - Using 0 as the fallback in averages. Failed rows become zeros and lower the mean.
- Using
""and then doing arithmetic on the result. Text in a numeric chain produces new#VALUE!errors. - Putting a long formula inside IFERROR and testing only the happy path. Test it with a failing input to confirm the fallback appears.
- Expecting it to catch a wrong answer. IFERROR only reacts to error values. A formula that returns a wrong number is not an error.
A narrower alternative
When you only expect a zero divisor, test for that case directly:
=IF(B2=0, "", A2/B2)Now only the zero-divisor case is handled. Any other problem, such as text in A2, still shows its error and gets noticed.
Exercise
A sheet has =IFERROR(A2/B2, 0) in column C, and =AVERAGE(C2:C20) below it. Several rows have an empty B cell. The average looks too low. Why does this happen, and what could you change?
Show answer
Empty B cells make the division return #DIV/0!, so IFERROR returns 0 for those rows. AVERAGE counts those zeros as real values, which lowers the mean. Return "" instead, as in =IFERROR(A2/B2, ""), because AVERAGE ignores text. Alternatively, use =IF(B2=0, "", A2/B2) so only the zero-divisor case is handled and other errors stay visible.
Exercise
What does =IFERROR(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found") show if someone deletes column B and the lookup range becomes #REF!? Why is this a problem?
Show answer
It shows "Not found", the same message as for a genuinely missing key. The real problem, a broken range, is hidden, so the sheet looks as if the data is missing instead of the formula being damaged.