Sub topic 10
Preventing Errors with IF, ISNA and ISERROR
This chapter assumes you can enter and copy formulas, tell relative from absolute references, and write a basic IF and a VLOOKUP. IFERROR and IFNA fix an error after it happens. This chapter covers the other approach: test the condition first, so the error never occurs.
Why test first
IFERROR wraps a whole formula and replaces any error it produces. That is convenient, but it can hide mistakes you needed to see, such as a deleted column (#REF!) or a misspelled function (#NAME?).
A preventive check handles only the case you expect. Any other error still shows, so you notice it.
=IF(B2=0, "", A2/B2)This formula checks the divisor before dividing. If B2 is zero, it returns an empty string. Otherwise it divides. If A2 holds text, the division still returns #VALUE!, and that is the behavior you want, because the problem is real.
The three testing functions
Each function takes a value and returns TRUE or FALSE. You normally place one inside the condition of an IF.
| Function | Returns TRUE for | Typical use |
|---|---|---|
| ISNA(value) | #N/A only | Detecting a missing lookup match |
| ISERROR(value) | Any of the seven error values | Broad checks, similar to IFERROR |
| ISERR(value) | Any error except #N/A | Ignoring missing matches but catching other errors |
ISERROR behaves like IFERROR, so it has the same masking risk. Prefer the narrowest test that matches the failure you expect.
Checking whether a lookup will succeed
MATCH returns the position of a value in a list, or #N/A if the value is missing. That makes it a good test for existence.
=IF(ISNA(MATCH(E2, $A$2:$A$100, 0)), "Missing", "Found")The 0 forces an exact match. The formula returns a label and never shows an error. You can use the same test to guard a lookup:
=IF(ISNA(MATCH(E2, $A$2:$A$100, 0)), "Not found", VLOOKUP(E2, $A$2:$B$100, 2, FALSE))This runs the match twice, once in the test and once in the lookup. On large sheets that costs calculation time. IFNA, or XLOOKUP's fourth argument, does the same job with one lookup:
=XLOOKUP(E2, $A$2:$A$100, $B$2:$B$100, "Not found")Checking inputs before calculating
Many errors come from bad inputs, so you can test the inputs directly.
ISNUMBER(A2)is TRUE only when A2 holds a number. Text and blanks give FALSE.ISBLANK(B2)is TRUE only when B2 is truly empty. A cell holding""is not blank.COUNTIF($A$2:$A$100, E2)>0tests whether a value appears in a list without producing an error.
A guard that requires both inputs to be usable looks like this:
=IF(AND(ISNUMBER(A2), ISNUMBER(B2), B2<>0), A2/B2, "Check inputs")Here a text value in either cell returns a message instead of #VALUE!. Because the message is explicit, a reviewer can see which rows need attention.
Choosing between the approaches
The last branch matters most. If you cannot say why a formula might fail, hiding the error with ISERROR or IFERROR only postpones finding the cause.
Common mistakes
- Testing
B2=""when B2 holds a formula that returns an error. The comparison itself returns the error. Test with ISERROR or ISNA first. - Using ISBLANK on a cell that contains
"". It returns FALSE. UseB2=""instead. - Forgetting the exact-match argument.
MATCH(E2, A2:A100)defaults to approximate matching, which can return a wrong position instead of #N/A. - Returning
""and then using the cell in arithmetic. The empty string is text, so=C2+1returns #VALUE!.
Exercise
Column A holds quantities and column B holds prices. Some price cells contain the text "n/a". Write a formula for C2 that returns A2*B2 when both are numbers and the message "Check inputs" otherwise. Why is this better than IFERROR here?
Show answer
=IF(AND(ISNUMBER(A2), ISNUMBER(B2)), A2*B2, "Check inputs"). It tests exactly the condition you expect, bad inputs, so any other problem, such as a #REF! from a deleted column, still shows instead of being replaced by the message.