Sub topic 14
Preventing errors versus masking them
This chapter assumes you can enter formulas, use cell references and ranges, and have used SUM, AVERAGE, VLOOKUP, IF, IFERROR and IFNA. It also assumes you know the error values covered earlier, such as #DIV/0!, #VALUE!, #REF! and #N/A.
Two ways to deal with an error
An error value tells you something went wrong. You can respond in two ways:
- Prevent the error. Test the condition that causes it, or fix the data or formula behind it.
- Mask the error. Let the formula fail, then replace the result with a blank or a message.
Both are useful. The mistake is masking an error you should have investigated.
Prevention with IF
An IF test checks one specific, known cause before the formula runs.
=IF(B2=0,"",A2/B2)This formula only handles a zero or blank divisor. If A2 holds text, the formula still returns #VALUE!, and you still see it. That is the advantage of prevention: it handles the case you expected and leaves every other problem visible.
The same idea applies to #NUM!:
=IF(A2>=0,SQRT(A2),"")Masking with IFERROR and IFNA
IFERROR catches every error type. That includes errors you did not expect.
=IFERROR(AVERAGE(B2:B10),"No data")Here the only likely error is #DIV/0! from an empty range, so a "No data" message is reasonable.
IFNA is narrower. It replaces only #N/A, which a lookup returns when it finds no match.
=IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found")If someone deletes a column and the lookup returns #REF!, this formula still shows #REF!. You see the damage instead of a friendly "Not found".
How masking goes wrong
Consider a formula wrapped in IFERROR:
=IFERROR(VLOOKUP(E2,A2:B100,3,FALSE),"Not found")The column index 3 is larger than the two columns in A2:B100, so the lookup always fails with #REF!. IFERROR turns that into "Not found" on every row. The sheet looks like the products are missing from the list, when the formula is broken. Nobody sees an error, so nobody fixes it.
Choosing an approach
| Situation | Better choice | Reason |
|---|---|---|
| Divisor may be zero or blank | IF test on the divisor | Only the known cause is handled |
| Lookup may find no match | IFNA | Other errors such as #REF! stay visible |
| Average of a range that may be empty | IFERROR or an IF test | Only one error is realistic |
| Formula never failed before and now does | No wrapper. Investigate | The error signals a real change |
| Report for readers who should not see error codes | Prevent first, then mask what remains | Clean output without hiding bugs |
A practical routine
- Build the formula without any wrapper and test it on normal and edge-case data.
- Fix errors at the source. Clean text numbers with
=VALUE(TRIM(A2)), correct misspelled names, and repair broken references. - Add an IF test for causes you expect, such as a zero divisor.
- Use IFNA for lookups where "no match" is a valid result.
- Use IFERROR only where you know exactly which error can occur.
- Return a message such as "Not found" or "No data" rather than an empty string when a blank could be mistaken for a real value.
Masked errors still affect other cells
Replacing an error with "" produces text, not a number. A later formula such as =A2+B2 returns #VALUE! if either cell holds that text. =SUM(A2,B2) ignores the text and continues. Check how downstream formulas treat your replacement value, since a masked error can reappear somewhere else.
Exercise
A sheet uses =IFERROR(VLOOKUP(E2,A2:B100,2,FALSE),"Not found"). After a colleague deletes column B, every row shows "Not found". What went wrong, and how would you change the formula so the problem is visible?
Show answer
The lookup now returns #REF! because its table range is invalid, and IFERROR converts every error to "Not found". Use IFNA instead: =IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found"). Then genuine missing matches show "Not found", while #REF! stays visible so you can repair the reference.
Exercise
You need =A2/B2 to show a blank only when B2 is zero or empty, but still reveal other problems such as text in A2. Which approach do you use?
Show answer
Use an IF test: =IF(B2=0,"",A2/B2). It handles only the zero or blank divisor. If A2 contains text, the formula returns #VALUE!, which stays visible.