Sub topic 14
Preventing errors versus masking them
This chapter assumes you can enter and edit formulas, use cell references and ranges, and use basic functions such as SUM, AVERAGE, VLOOKUP, IF, IFERROR and IFNA. It also assumes you have met the main error values (#DIV/0!, #VALUE!, #REF!, #NAME?, #N/A, #NUM!, #NULL!, #SPILL!) and the debugging tools. Here you learn to decide, for each error, whether to prevent it, handle it, or leave it visible.
Two different goals
When a formula returns an error, you have three choices:
- Prevent the error by fixing the formula or the data so the failing condition never occurs.
- Handle an error that is expected and legitimate by returning a planned result, such as a blank or the text "Not found".
- Mask an error by wrapping the formula so the error message disappears, without knowing why it happened.
Prevention and handling are good practice. Masking is the risk. A masked error is not fixed. The worksheet only stops telling you about it.
An error value is information. It tells you that a formula could not produce a valid answer. If you hide that signal, a wrong total can look like a correct one.
Why masking is dangerous
Consider this formula:
=IFERROR(AVERAGE(B2:B10),"No data")This is a reasonable use. AVERAGE returns #DIV/0! only when the range holds no numbers. That is a known, expected situation, and the message "No data" describes it accurately.
Now compare a broad wrapper around a lookup:
=IFERROR(VLOOKUP(E2,A2:B100,3,FALSE),"Not found")This formula has a bug. The column index 3 is larger than the two columns in A2:B100, so VLOOKUP returns #REF!. IFERROR catches every error type, so every row shows "Not found". The result looks like a data problem ("those items are missing") when the real problem is a mistake in the formula. Nobody sees a red flag, and the report goes out with wrong results.
The cause is that IFERROR does not distinguish between errors. It treats #N/A, #REF!, #NAME?, #VALUE! and #DIV/0! the same way.
Choosing the narrowest tool
Pick the tool that catches only the situation you expect. The table lists the options from narrowest to broadest.
| Situation you expect | Narrow tool | What still shows |
|---|---|---|
| Divisor may be zero or blank | `IF` test on the divisor | Every other error |
| Lookup may find no match | `IFNA` | #REF!, #NAME?, #VALUE! and others |
| Any error is acceptable to hide | `IFERROR` | Nothing |
Prevent with a test: IF
An IF test checks the condition that causes the error, before the error can happen.
=IF(B2=0,"",A2/B2)This returns an empty string when B2 is zero or blank, and divides normally otherwise. If A2 holds text, the division still returns #VALUE!. That is the correct outcome, because the text is a real data problem the formula should not hide.
Another example avoids #NUM! by testing the argument first:
=IF(A2>=0,SQRT(A2),"")Both formulas name the exact condition they handle. Anyone reading them can see what the author expected.
Handle one error type: IFNA
For lookups, a missing match is often normal. Use IFNA, which replaces only #N/A:
=IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found")If someone later breaks the formula, for example by changing the column index to 3, the #REF! error still appears. You find the problem the first time you look at the sheet.
Mask everything: IFERROR
IFERROR is acceptable when every possible error has the same meaning for your purposes, and the formula is simple enough that you can be sure of that. The AVERAGE example above qualifies. A long formula that combines several lookups and calculations usually does not, because many different failures could hide behind one message.
Prevent errors at the source
Often the best fix is not in the formula at all. It is in the data or the structure of the workbook.
- Clean the inputs. Many #VALUE! errors come from numbers stored as text or from stray spaces. Fix the source data once instead of wrapping every formula. For one-off cleanup,
=VALUE(TRIM(A2))converts the text into a real number. - Use operators and functions that tolerate text.
=A2+B2returns #VALUE! when a cell holds text such as "n/a", but=SUM(A2,B2)ignores text in cell references. Choose SUM when text is possible and ignoring it is correct. - Validate entries. Data validation can stop users from typing text into a numeric column or leaving a required divisor blank.
- Avoid fragile references. Deleting rows or columns that formulas point to causes #REF!. Review dependents with Trace Dependents before deleting.
- Write function names and ranges carefully. A typo such as
=SUMM(A1:A5)returns #NAME?. Use the function-name suggestions that appear as you type. - Use the right range operator.
=SUM(A1:A5,C1:C5)uses a comma.=SUM(A1:A5 C1:C5)uses a space, which is the intersection operator and returns #NULL! when the ranges do not overlap.
A decision process
Use this sequence each time you are tempted to wrap a formula.
- Find the cause. Use Error Checking, Trace Precedents and Evaluate Formula to find which part of the formula fails and why.
- Ask whether it is a bug. If the formula or the data is wrong, fix it. Do not wrap it.
- Ask whether it is expected. If the condition is normal, such as a blank divisor or an item not in a list, decide how the sheet should show it.
- Choose the narrowest handler. Prefer an IF test on the cause, then IFNA for lookups, and IFERROR only when nothing else fits.
- Make the result honest. Return text that says what happened ("No data", "Not found") when a reader needs to know. Return an empty string only when a blank is clearly understood.
Side effects of blanks and text results
Handling an error often means returning "". This looks blank, but it is a text value of zero length, not an empty cell. Later formulas can then fail in new ways:
=C2*2returns #VALUE! when C2 holds"", because the formula tries to multiply text.- SUM, AVERAGE and COUNT ignore the empty string in a range, so totals still work.
- A chart may plot
""as zero.
A handled error in one column can therefore produce a new error in another. When later calculations depend on the result, consider returning a number such as 0 only if zero is a true answer. Otherwise use NA() deliberately, because charts skip #N/A points, and handle it downstream with IFNA.
Document what you hide
If you do mask or handle an error, leave a trail:
- Use a specific message instead of a blank, so readers know the cell was handled on purpose.
- Add a note or a cell comment explaining the rule, such as "Blank when no sales were recorded."
- Add a check cell that counts problems, for example the number of lookups that returned "Not found", so a sudden jump gets noticed.
Common mistakes
- Wrapping the whole sheet in IFERROR to make it look clean. This hides real bugs.
- Using IFERROR around a long formula. If any step fails, you cannot tell which one. Test the specific input instead.
- Returning 0 for a missing value. A zero looks like real data and changes totals and averages.
- Fixing the symptom, not the data. If dozens of cells need
TRIMandVALUE, clean the source column once. - Forgetting that a handler changes the type. An empty string breaks arithmetic in later formulas.
Exercise
A colleague wrote =IFERROR(VLOOKUP(E2,A2:B100,3,FALSE),"Not found") and every row says "Not found", even for items that exist in the list. What is wrong, and how would you rewrite the formula so this kind of mistake stays visible?
Show answer
The column index 3 is larger than the two columns in A2:B100, so VLOOKUP returns #REF!. IFERROR catches it and shows "Not found", which hides the bug. Change the index to 2 and use IFNA so only a genuine missing match is replaced: =IFNA(VLOOKUP(E2,A2:B100,2,FALSE),"Not found"). If the range is wrong again, #REF! will show.
Exercise
You need a percentage in column C equal to A2/B2. B2 is sometimes zero or blank, which is expected, but it can also contain the text "n/a" by mistake, which you want to notice. Write a formula and explain why it is better than IFERROR here.
Show answer
Use =IF(B2=0,"",A2/B2). It handles only the zero or blank divisor. If B2 holds "n/a", the division returns #VALUE!, which stays visible so the data problem gets fixed. IFERROR would hide both cases and make the bad entry invisible.
Exercise
A formula returns "" for missing data, and another cell computes =C2*2 from it. The second cell shows #VALUE!. Why, and what are two ways to fix it?
Show answer
The empty string is text, and multiplying text returns #VALUE!. One fix is to test the input in the second formula, such as =IF(C2="","",C2*2). Another is to change the first formula so it returns a real value or NA() and handle that with IFNA downstream. Returning 0 is only right if zero is a true answer.