Sub topic 10
Catching errors with IFERROR
This chapter assumes you can enter formulas, use cell references such as B2, and recognize basic spreadsheet errors. You will use IFERROR to replace an error result with a useful fallback, such as a message or blank cell.
What IFERROR does
An error can interrupt a report or make a column difficult to read. For example, dividing by zero produces #DIV/0!:
=B2/C2If C2 contains 0, the formula cannot calculate a result. IFERROR tests whether a formula produces an error:
=IFERROR(B2/C2, "Check data")The first argument is the calculation to try. The second argument is the result to return if that calculation fails.
| Formula | Result when no error occurs | Result when an error occurs |
|---|---|---|
| `=IFERROR(B2/C2, "Check data")` | The quotient | `Check data` |
If B2 is 80 and C2 is 100, the result is 0.8. If C2 is 0 or contains incompatible text, the result is Check data.
Choosing a useful fallback
The fallback can be text, a number, an empty string, or another formula.
=IFERROR(B2/C2, 0)This returns 0 when the calculation fails. Use this only when zero has a meaningful interpretation. A failed calculation and a true zero are usually different facts.
To leave the cell visually blank, return an empty string:
=IFERROR(B2/C2, "")An empty string is text with no visible characters. It is useful in reports, but it is not the same as an actually empty cell. Some functions and charts may treat the two differently.
You can also return a formula result:
=IFERROR(VLOOKUP(A2, Products!A:B, 2, FALSE), "Product not found")If the lookup finds a matching product, the formula returns its price. If the lookup produces an error, it returns Product not found.
IFERROR with IF
IFERROR can wrap an IF formula when the condition includes a calculation that may fail:
=IFERROR(IF(B2/C2>0.5, "High", "Low"), "Check data")The inner IF compares the ratio with 0.5. If C2 is zero, the division fails before the comparison can produce TRUE or FALSE. The outer IFERROR catches that failure.
This arrangement separates two jobs:
IFchooses between normal outcomes.IFERRORhandles a calculation that could not produce a valid result.
The order matters. If you write only IF(B2/C2>0.5, "High", "Low"), a zero denominator still displays an error.
Avoid hiding real problems
IFERROR catches every error from its first argument. That makes it convenient, but it can hide a mistake in the formula itself.
For example:
=IFERROR(B2/C2, "Check data")This formula treats a zero denominator, misspelled sheet reference, and invalid cell reference as the same situation. If you expected a lookup to find every product, a fallback message may conceal missing product IDs or a wrong table range.
Use a fallback that helps you investigate:
=IFERROR(VLOOKUP(A2, Products!A:B, 2, FALSE), "Missing product: " & A2)During development, first inspect the unwrapped formula. Add IFERROR after you understand which errors are expected.
Common choices
- Use a message when someone needs to correct the source data.
- Use
""when an error should not appear in a presentation column. - Use
0only when downstream calculations should treat the failure as zero. - Avoid returning a normal-looking value that could be mistaken for valid data.
Exercise
Write a formula that calculates the completion rate in B2 divided by C2, returns "Complete" when the rate is at least 1, "In progress" otherwise, and returns "Enter a valid target" if the calculation errors.
Show answer
=IFERROR(IF(B2/C2>=1, "Complete", "In progress"), "Enter a valid target")