Sub topic 10
Handling rows with no match using IFERROR
This chapter assumes you can enter formulas, reference ranges, and write REGEXEXTRACT patterns with one match, as covered in the previous chapter. You should also know that IFERROR is a function that takes a value and a fallback.
By the end you will be able to:
- Explain why
REGEXEXTRACTreturns an error for some rows. - Replace that error with a blank, a label, or a number.
- Choose between
IFERRORandIFNA. - Avoid hiding real mistakes in your formula.
Why a row can fail to match
REGEXEXTRACT looks for the first piece of text that fits your pattern. If nothing fits, it has nothing to return, so it returns the #N/A error. The error means "no value available." It does not mean your formula is broken.
Take this column of messy notes and the formula =REGEXEXTRACT(A2, "\d+"), which extracts the first run of digits:
| Row | A (input) | Result of `REGEXEXTRACT(A, "\d+")` |
|---|---|---|
| 2 | Invoice 4021 paid | 4021 |
| 3 | no number here | #N/A |
| 4 | (empty cell) | #N/A |
| 5 | Ref 17 / 88 | 17 |
| 6 | Total: 305 USD | 305 |
Rows 3 and 4 have no digits, so both return #N/A. An empty cell counts as text with no match, so it fails the same way.
Real columns often contain a mix of matching and non-matching rows. If you leave the errors in place, three things go wrong:
- The sheet looks broken.
- Any formula that reads the column, such as
SUMorAVERAGE, may return an error too. - Sorting and filtering get harder.
Replacing the error with IFERROR
IFERROR(value, value_if_error) evaluates its first argument. If that argument produces any error, IFERROR returns the second argument instead. If there is no error, it passes the first result through unchanged.
=IFERROR(REGEXEXTRACT(A2, "\d+"), "")This returns an empty string when the cell has no digits. For row 3 you see a blank cell. For row 2 you still see 4021.
The second argument is optional. If you omit it, IFERROR returns a blank:
=IFERROR(REGEXEXTRACT(A2, "\d+"))I recommend writing the "" anyway. It shows the reader that a blank is intended.
Choosing a fallback value
The fallback is whatever fits the next step in your sheet. Common choices:
| Fallback | Formula | Use it when |
|---|---|---|
| Empty string | `=IFERROR(REGEXEXTRACT(A2, "\d+"), "")` | The column is for display, or you will filter blanks later. |
| A label | `=IFERROR(REGEXEXTRACT(A2, "\d+"), "no number")` | A person needs to see which rows failed. |
| Zero | `=IFERROR(VALUE(REGEXEXTRACT(A2, "\d+")), 0)` | Later math must treat a missing number as 0. |
| Another extraction | See the next section. | The data has more than one known format. |
The empty string and the number zero behave differently. A "" is text that looks empty. Arithmetic on it, such as =B2*2, gives a #VALUE! error, while SUM and AVERAGE skip it. A real 0 takes part in calculations and pulls an AVERAGE down. If a missing value should not count, use "". If it should count as zero, use 0.
The VALUE function in the zero example turns the extracted text into a number. The chapter on type conversion covers it in detail.
Trying a second pattern as a fallback
The fallback can be another formula. This lets you handle a column where IDs appear in more than one format:
=IFERROR(REGEXEXTRACT(A2, "ORD-(\d+)"), IFERROR(REGEXEXTRACT(A2, "#(\d+)"), ""))Sheets tries the first pattern. If it fails, it tries the second. If that also fails, it returns a blank. Each IFERROR handles one level, so the outer one needs a closing parenthesis for each one nested inside it.
Keep nesting short. More than two or three levels is hard to read. If you need that many formats, write the patterns into one regular expression with | (a vertical bar meaning "or") instead:
=IFERROR(REGEXEXTRACT(A2, "(?:ORD-|#)(\d+)"), "")Here (?:...) groups the alternatives without capturing them. Only (\d+) is captured and returned.
Applying the fix to a whole column
You can wrap an array formula in IFERROR the same way you wrap a single formula:
=ARRAYFORMULA(IFERROR(REGEXEXTRACT(A2:A10, "\d+"), ""))One formula in one cell fills the whole column. Rows that match show the digits and rows that do not show a blank.
Put ARRAYFORMULA outside and IFERROR inside it, as shown. This ordering makes the error check run on each row separately.
Fallbacks and capture groups
A pattern with several capture groups returns several cells. For text like ORD-10492, this formula returns ORD and 10492 in two adjacent cells:
=REGEXEXTRACT(A2, "([A-Z]+)-(\d+)")If you wrap it with a single fallback, only the first cell gets that fallback when the match fails:
=IFERROR(REGEXEXTRACT(A2, "([A-Z]+)-(\d+)"), "no match")On a failing row, you get "no match" in the first cell and nothing in the second. That is often fine. To fill both cells, give the fallback the same width as the result by using an array literal. In an array literal, commas separate columns:
=IFERROR(REGEXEXTRACT(A2, "([A-Z]+)-(\d+)"), {"none", "none"})Now both output cells show "none" when there is no match.
IFERROR versus IFNA
IFERROR catches every error type: #N/A, #VALUE!, #DIV/0!, #REF!, and others. IFNA catches only #N/A. A failed match produces #N/A, so either function works for the cases in this chapter.
=IFNA(REGEXEXTRACT(A2, "\d+"), "")Prefer IFNA when you can. A pattern with a typo, such as an unclosed parenthesis, produces a different error, typically #VALUE!. IFNA lets that error through, so you notice the mistake. IFERROR converts it into a blank, and the formula looks like it works while returning nothing for every row.
Writing and testing the formula in stages
Build the formula without a wrapper first, then add the wrapper once the pattern works:
- Write
=REGEXEXTRACT(A2, "\d+")and fill it down. Rows with a clear match should show the right text. - Look at the
#N/Arows. Confirm each one really has nothing to match. If a row should have matched, fix the pattern, not the fallback. - Wrap the formula in
IFNAorIFERROR. - Fill down again and compare against step 1. Only the
#N/Arows should change.
Finding which rows failed
Sometimes you want to know which rows failed, not hide them. ISNA returns TRUE when its argument is #N/A:
=ARRAYFORMULA(ISNA(REGEXEXTRACT(A2:A10, "\d+")))This gives TRUE for rows with no match and FALSE for the others. You can use it in conditional formatting to highlight bad rows, or count them:
=COUNTIF(ARRAYFORMULA(ISNA(REGEXEXTRACT(A2:A10, "\d+"))), TRUE)Or you can pull out the matching rows only by combining it with FILTER:
=FILTER(A2:A10, NOT(ISNA(REGEXEXTRACT(A2:A10, "\d+"))))This returns only the notes that contain digits.
Common mistakes
- Using
IFERRORbefore the pattern is tested. A bad pattern then looks like "no matches found." - Expecting
""to behave like a number. A blank string causes#VALUE!in arithmetic such as=B2+1. - Forgetting that an empty source cell also returns
#N/A. If blank input should be a separate case, check for it first, for example=IF(A2="", "empty", IFNA(REGEXEXTRACT(A2, "\d+"), "no number")). - Placing the fallback inside the regular expression. The second argument of
REGEXEXTRACTis the pattern only. The fallback belongs toIFERROR. - Assuming a partial match counts as no match.
REGEXEXTRACTmatches anywhere in the text, so"\d+"finds digits insideabc123def. If you want the whole cell to match, anchor the pattern with^and$.
Summary
REGEXEXTRACT returns #N/A when the pattern does not appear in the text, including when the cell is empty. IFERROR(formula, fallback) replaces any error with the fallback, and IFNA replaces only #N/A, which makes it the safer choice for matching. Choose the fallback by what comes next: "" for display, a label for review, 0 for math. Wrap an array formula as ARRAYFORMULA(IFERROR(...)) to handle a whole column. Test the bare pattern first so the wrapper does not hide a mistake.
Exercise
Column A holds order notes such as "Order 5531 shipped", "waiting for payment", and "Order 88 cancelled". Write one formula in B2 that fills B2:B4 and shows the first number in each note. Rows with no number must show the text "none". Use the function that catches only no-match errors.
Show answer
=ARRAYFORMULA(IFNA(REGEXEXTRACT(A2:A4, "\d+"), "none")). The results are 5531, none, and 88. IFNA catches only the #N/A that REGEXEXTRACT returns when no digits are found, so a typo in the pattern would still show an error.
Exercise
A formula =IFERROR(REGEXEXTRACT(A2, "(\d+"), "") returns a blank on every row, even rows with digits. What is wrong, and how do you find it?
Show answer
The pattern "(\d+" has an opening parenthesis that is never closed, so it is not a valid regular expression. REGEXEXTRACT returns an error on every row, and IFERROR turns each one into a blank. Remove the IFERROR wrapper to see the real error, then fix the pattern to "(\d+)" or "\d+". Using IFNA instead would have let the error show.
Exercise
Cell A2 contains "ORD-10492" or, in some rows, "no order". You use =IFERROR(REGEXEXTRACT(A2, "([A-Z]+)-(\d+)"), "n/a"). For the row "no order", how many cells show "n/a", and how do you make both output cells show it?
Show answer
Only one cell, the first, shows "n/a". The scalar fallback fills just one cell. To fill both, use an array literal as the fallback: =IFERROR(REGEXEXTRACT(A2, "([A-Z]+)-(\d+)"), {"n/a", "n/a"}).