Sub topic 10
Preventing Errors with IF, ISNA and ISERROR
This chapter assumes you can enter and copy formulas, use relative and absolute references, and write a basic IF and VLOOKUP. It also assumes you have met IFERROR and IFNA in the earlier chapters. Here you will learn to stop an error before it happens, or to test for it explicitly, so that only the problems you expect are handled and the rest stay visible.
Prevention versus catching
IFERROR and IFNA are catching tools. They run the calculation, see that it failed, and swap in a replacement. A preventive check works the other way. It tests a condition first and only runs the calculation if the condition is safe.
The difference matters because a catching function reacts to the symptom, and a preventive check reacts to a specific cause. When you write =IF(B2=0, "", A2/B2), you are saying: "I know a zero divisor is a legitimate case here, and I want to handle that one case." Any other problem, such as text in B2 or a deleted column, still produces a visible error.
| Approach | Reacts to | Other errors | Typical use |
|---|---|---|---|
| IFERROR | Any error value | Hidden | Quick cleanup where any failure has the same meaning |
| IFNA | Only #N/A | Visible | Lookups where "not found" is expected |
| IF with a test | One named condition | Visible | Known edge cases such as a zero divisor |
| IF with ISNA or ISERROR | A test on a result | Depends on the function used | Branching on whether a result exists |
Prevent #DIV/0! with an IF test
The most common preventable error is division by zero. Test the divisor before you divide.
=IF(B2=0, "", A2/B2)If B2 is zero, the formula returns an empty string. Otherwise it returns A2/B2. Compare this with =IFERROR(A2/B2, ""). Both look the same when B2 is zero. They differ when B2 contains the text "n/a". Then A2/B2 produces #VALUE!. The IFERROR version hides it. The IF version does not, because the text is not equal to zero, so the division runs and the #VALUE! shows. That is the behavior you want, since text in a numeric column is a data problem you should see.
Be careful with what B2=0 means for blanks. An empty cell is treated as 0 in this comparison, so a blank divisor is caught by the same test. If you want a blank divisor to behave differently from a zero divisor, test for it separately:
=IF(B2="", "No data", IF(B2=0, "Zero base", A2/B2))Notice that the order matters. An empty cell satisfies both B2="" and B2=0, so the blank test must come first, otherwise the "Zero base" message would catch it.
Testing several conditions
When a formula can fail for more than one expected reason, nest the checks or combine them with AND and OR. Here the formula requires both cells to hold numbers and the divisor to be non-zero:
=IF(AND(ISNUMBER(A2), ISNUMBER(B2)), IF(B2=0, "", A2/B2), "Check inputs")Two things to know about this pattern:
- ISNUMBER returns FALSE for text, for blanks, and for error values. It never returns an error itself, so it is safe to use as a gate.
- AND does not stop at the first FALSE. It evaluates every argument. This is why the zero test sits in a nested IF rather than inside the AND. If you wrote
AND(ISNUMBER(B2), B2<>0), a text value in B2 would still be compared to 0 without an error, but a cell holding an error value would make the whole AND return that error.
ISNA: test for a missing lookup
ISNA takes one value and returns TRUE if that value is the #N/A error, and FALSE for anything else, including other errors. It never returns an error itself.
You can use it to branch on whether a lookup found something:
=IF(ISNA(MATCH(E2, $A$2:$A$100, 0)), "Missing", "Found")MATCH with match type 0 looks for an exact match and returns the position, or #N/A if the value is absent. ISNA turns that into TRUE or FALSE, and IF picks a label.
This pattern is useful when you want the label itself, not a lookup result. Examples are a "Found/Missing" audit column, a conditional formatting rule, or a filter helper column.
The old double-lookup pattern
Before IFNA existed, a lookup with a fallback required running the lookup twice:
=IF(ISNA(VLOOKUP(E2, $A$2:$B$100, 2, FALSE)), "Not found", VLOOKUP(E2, $A$2:$B$100, 2, FALSE))This works, but it has three drawbacks. The formula is long. Excel does the lookup twice, which is slow on large ranges. And if you change one lookup and forget the other, the two halves disagree. If you have access to IFNA, use it instead:
=IFNA(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found")You will still meet the double-lookup form in workbooks created before Excel 2013, so you should be able to read it.
ISERROR and ISERR
ISERROR returns TRUE for any error value, including #N/A. It is the test version of IFERROR.
=IF(ISERROR(A2/B2), "Problem", A2/B2)This has the same masking problem as IFERROR, and it also repeats the calculation. For these reasons, prefer IFERROR when you simply want a fallback. ISERROR is still useful when you want to branch in a way IFERROR cannot, for example to return different kinds of output depending on whether a result is an error:
=IF(ISERROR(F2), 1, 0)Placed in a helper column, this flags error rows with a 1 so you can sum them and see how many rows failed.
ISERR is a close relative. It returns TRUE for every error except #N/A. Together the three functions divide the space of errors cleanly:
| Function | TRUE for #N/A | TRUE for other errors | TRUE for normal values |
|---|---|---|---|
| ISNA | Yes | No | No |
| ISERR | No | Yes | No |
| ISERROR | Yes | Yes | No |
ISERR is rarely needed, but it gives you a way to detect "something is wrong that is not a simple missing lookup". For example, a quality-check column could be =ISERR(G2), and any TRUE in it points to a problem such as #REF! or #VALUE! that you should investigate rather than suppress.
Choosing a check
The decision is about what you know in advance.
- If you know the exact cause and it is legitimate, test for it with IF. Zero divisors and empty inputs are the typical cases.
- If the only expected failure is a missing lookup value, use IFNA, or XLOOKUP's
if_not_foundargument. - If you need a Found/Missing label or a flag rather than a looked-up value, use IF with ISNA.
- If you want to count or highlight every error in a range, use ISERROR or ISERR in a helper column or a conditional formatting rule.
- Use IFERROR only when any failure truly has one meaning and you accept that real mistakes will be hidden.
Using XLOOKUP instead of a check
In Excel 365 and Excel 2021, XLOOKUP has a built-in fourth argument for the missing case:
=XLOOKUP(E2, $A$2:$A$100, $B$2:$B$100, "Not found")This replaces the pattern of wrapping a lookup in a test. It handles only a missing match. Other errors, such as a #REF! from a deleted range, still appear. In that sense it behaves like IFNA, and it avoids both the double evaluation and the extra function.
Edge cases and gotchas
An empty string is text
When a preventive check returns "", the cell looks empty but contains text of length zero. Downstream formulas treat it differently from a true blank:
=A2+1where A2 holds""gives #VALUE!.=SUM(A1:A5)ignores the text, so totals still work.=COUNTA(A1:A5)counts the cell, because it is not empty.=ISBLANK(A2)returns FALSE.
If other formulas do arithmetic directly on the cell with +, return 0 instead, and use a number format to hide the zeros if they look cluttered.
A check can be wrong in a different way
Prevention only works if the condition matches the real cause. Consider =IF(B2=0, "", A2/B2) where B2 contains the text "0" typed with an apostrophe. The text "0" is not equal to the number 0, so the check passes, the division runs, and you get #VALUE!. That is the formula doing its job, since it shows you the data-entry problem. But it surprises people who assumed the zero test covered every "zero-looking" input.
Errors inside the test itself
If the value you are testing is already an error, an IF comparison on it returns that error. For example, if B2 contains #N/A, then B2=0 evaluates to #N/A and the whole IF returns #N/A. Only the IS functions are safe to run on a cell that may hold an error. When an upstream cell can be an error, gate it first:
=IF(ISERROR(B2), "Upstream error", IF(B2=0, "", A2/B2))Exact match on lookups
ISNA only catches a missing value if the lookup is set up correctly. A VLOOKUP with the last argument omitted or TRUE does an approximate match and can return a wrong value instead of #N/A. For a presence test, always use an exact match: FALSE in VLOOKUP, or 0 in MATCH.
Worked example: a budget variance column
You have actual spend in column B and budget in column C. You want the variance percentage in D, and you want to distinguish three situations: no budget set, a zero budget, and a real number.
=IF(C2="", "No budget", IF(C2=0, "Zero budget", (B2-C2)/C2))Reading it left to right:
- If C2 is empty, return "No budget".
- Otherwise, if C2 is zero, return "Zero budget".
- Otherwise, compute the variance.
If someone pastes the word "TBD" into C2, the first two tests pass and the calculation produces #VALUE!. You see the problem and fix the cell. Had you used IFERROR, "TBD" would have been silently replaced and the report would look clean while being wrong.
To total the numeric variances in a summary row, =SUM(D2:D50) ignores the text messages. To average them, =AVERAGE(D2:D50) also ignores the text, so rows with messages are left out of the average and do not count as zeros.
Exercise
Column A holds sales and column B holds units, and you want price per unit as A2/B2. Write a formula that returns a blank when B2 is zero or empty, but still shows #VALUE! if B2 contains text such as "n/a". Then explain why =IFERROR(A2/B2, "") does not behave the same way.
Show answer
Use =IF(B2=0, "", A2/B2). An empty cell compares as equal to 0, so both blank and zero divisors return an empty string. Text such as "n/a" is not equal to 0, so the division runs and returns #VALUE!, which stays visible. =IFERROR(A2/B2, "") catches every error type, so it would also replace the #VALUE! caused by the text with an empty string and hide the data problem.
Exercise
A list of order IDs is in A2:A100 and you want a helper column that shows "Found" or "Missing" for the ID in E2, without returning any looked-up data. Which formula would you write, and why use ISNA rather than ISERROR?
Show answer
Use =IF(ISNA(MATCH(E2, $A$2:$A$100, 0)), "Missing", "Found"). MATCH with match type 0 returns #N/A when the ID is absent, and ISNA is TRUE only for that error. ISERROR would also report "Missing" for other errors, such as #REF! from a broken range, and so would label a broken formula as a missing ID instead of letting the real error appear.
Exercise
Cell B2 contains #N/A because of an upstream lookup. What does =IF(B2=0, "", A2/B2) return, and how would you change it to report the upstream failure clearly?
Show answer
It returns #N/A, because the comparison B2=0 evaluates an error and the IF passes the error through. Wrap the check in an IS function, which can safely test an error: =IF(ISERROR(B2), "Upstream error", IF(B2=0, "", A2/B2)).