Sub topic 13
Pitfalls: blocked spill ranges, whitespace, and escaping
This chapter assumes you can write SPLIT, JOIN, and REGEXEXTRACT formulas, wrap them in ARRAYFORMULA, and use TRIM and IFERROR. It covers the failures that come up most often when those formulas run on real data, and how to diagnose each one.
Pitfall 1: a blocked spill range
SPLIT, TRANSPOSE, and ARRAYFORMULA all return more than one cell. The formula sits in one cell and writes its results into the cells to the right or below. If any of those cells already holds data, Sheets refuses to write any of the results. The formula cell shows #REF!, and the error message reads like this:
Array result was not expanded because it would overwrite data in B2.Here is a typical way to hit it. Column A holds red,green,blue, and you type this in B2:
=SPLIT(A2, ",")The formula needs B2, C2, and D2. If D2 contains a note you typed last week, the whole result is blocked, including B2 and C2.
Check these causes in order:
- A typed value in the path. Delete it or move the formula.
- A cell that looks empty but is not. A single space, or a formula returning
"", counts as data. Select the cells and press Delete to clear them. - A neighboring column.
=ARRAYFORMULA(SPLIT(A2:A10, ","))needs as many columns as the longest row produces. If column D holds other data, any row with four or more pieces fails, and so does the whole formula. - Two spilling formulas colliding. If you fill down
=ARRAYFORMULA(SPLIT(A2:A10, ","))instead of entering it once, the copy in row 3 collides with the spill from row 2.
The pieces per row can vary from row to row, so the width of the result is not fixed. Leave plenty of free columns to the right of a SPLIT, or put the formula in the last used column of the sheet. Enter an ARRAYFORMULA once, in the top cell of the block it fills.
Pitfall 2: whitespace you cannot see
Leading spaces after a comma
A list like apple, banana, cherry is the most common case. Splitting on "," leaves a leading space on the second and third pieces:
=SPLIT(A2, ",")The results are apple, banana, and cherry. The extra spaces break lookups, COUNTIF, and sorting. There are two fixes, and they behave differently:
=ARRAYFORMULA(TRIM(SPLIT(A2, ",")))=SPLIT(A2, ", ", FALSE)The first fix works whether or not the spaces are there. The second fix splits only on the exact pair "comma, space". A row written as apple,banana would then not split at all. Prefer TRIM when the source is typed by people. TRIM also collapses runs of internal spaces to one, so New York becomes New York.
The non-breaking space
Text pasted from web pages and PDFs often contains character 160, the non-breaking space. It looks like a space, but TRIM does not remove it. If a TRIM does nothing, replace that character first:
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))To confirm the cause, compare lengths. If LEN(A2) differs from LEN(TRIM(A2)), TRIM found ordinary spaces. If the lengths are equal but the text still looks padded, list the character codes:
=ARRAYFORMULA(UNICODE(MID(A2, SEQUENCE(LEN(A2)), 1)))This spills one code per character down the column. A 32 is an ordinary space and a 160 is a non-breaking space. Other invisible characters, such as the zero-width space (8203), show up the same way. Remove them with SUBSTITUTE(A2, UNICHAR(8203), "").
Empty pieces
By default SPLIT drops empty pieces, so a,,b gives a and b in adjacent cells. The gap disappears, so columns can shift left. If position matters, such as a fixed layout where the third field is always the phone number, set remove_empty_text to FALSE:
=SPLIT(A2, ",", TRUE, FALSE)Numbers and dates that SPLIT converts
SPLIT converts pieces that look like numbers or dates into real numbers and dates. 007 becomes 7 and the leading zeros are lost. A piece such as 3-4 may turn into a date. Formatting the destination cells as plain text afterward does not bring the zeros back.
When the pieces are identifiers, extract them with REGEXEXTRACT instead, because it returns text:
=REGEXEXTRACT(A2, "(\d+)$")Verify with =ISTEXT(B2) before relying on it. When you do want numbers from extracted text, convert them on purpose with VALUE, as shown in the earlier chapter on type conversion.
Pitfall 3: escaping
Three separate escaping rules apply, and mixing them up causes most of the confusing errors in this area.
Quotes inside a string
A formula string ends at the next double quote. To put a quote character inside a string, write it twice. To split on a double quote, the delimiter is a quote wrapped in a quote pair:
=SPLIT(A2, """")The outer quotes open and close the string, and "" inside means one literal quote. The same rule applies inside a regular expression, where you might write """(.*?)""" to extract text between quotes.
Backslashes are not special to Sheets
Many languages make you write "\\d" to get \d. Google Sheets does not. A backslash in a formula string is passed unchanged to the regular expression engine, so "\d+" is correct. Doubling it, as in "\\d+", asks the engine for a literal backslash followed by d characters, and that usually returns #N/A.
Delimiters are literal, patterns are not
SPLIT treats its delimiter as plain text. SPLIT(A2, ".") splits at every period, and SPLIT(A2, "|") splits at every pipe character. No escaping is needed.
REGEXEXTRACT treats its second argument as a regular expression, so these characters have special meaning and need a backslash to match literally:
. * + ? ( ) [ ] { } | ^ $ \| Goal | Wrong | Right |
|---|---|---|
| Extract a decimal such as `3.14` | `"\d+.\d+"` (the `.` matches any character) | `"\d+\.\d+"` |
| Extract text in parentheses | `"(.*)"` (the parentheses make a group, so the result drops them) | `"\((.*)\)"` |
| Match a literal `$` price | `"$\d+"` (`$` means end of text) | `"\$\d+"` |
For an unintended group, there is a second option. (?:...) groups without capturing, so it does not change what is returned.
If the pattern comes from a cell, for example a delimiter typed by a user, wrap the cell in \Q and \E so that RE2 treats everything between them as literal text:
=REGEXEXTRACT(A2, "\Q" & B1 & "\E" & "(\d+)")Be aware of what RE2 does not support. It has no lookahead, lookbehind, or backreferences. A pattern such as (?<=ID-)\d+ fails with an invalid-pattern error. Use a capture group instead: "ID-(\d+)".
Pitfall 4: regional settings
In locales that use a comma as the decimal separator, such as many European ones, the argument separator in formulas is a semicolon, and the column separator inside array literals changes as well. A formula copied from this tutorial, =SPLIT(A2, ","), may be rejected as a parse error. Check File, Settings, Locale. Change the locale, or replace commas that separate arguments with semicolons. Commas that sit inside quoted strings, such as the delimiter ",", stay as they are.
A quick symptom table
| Symptom | Likely cause | First thing to try |
|---|---|---|
| `#REF!` mentioning overwriting data | Something is in the spill path | Clear the cell named in the message |
| Pieces have leading spaces | Delimiter is `,` but source uses `, ` | `ARRAYFORMULA(TRIM(SPLIT(...)))` |
| TRIM changes nothing | Non-breaking space (code 160) | `SUBSTITUTE(A2, CHAR(160), " ")` |
| `007` shows as `7` | SPLIT converted the piece to a number | Extract with REGEXEXTRACT instead |
| `#N/A` from REGEXEXTRACT | No match, or a doubled backslash | Test the pattern on one cell, then use IFERROR |
| `#ERROR!` or invalid pattern | Lookaround or backreference used | Rewrite with capture groups |
| Formula rejected as unparsable | Locale uses `;` as separator | Match the locale's separator |
Exercise
Cell A2 holds red, green ,blue. You enter =SPLIT(A2, ",") in B2 and see #REF!. D2 contains the word "total". After you clear D2, the result appears but the second piece is green with spaces. Explain both problems and write one formula that fixes the second.
Show answer
The SPLIT needs B2, C2, and D2, and the text in D2 blocks the whole spill, so the result is #REF! until D2 is cleared. The spaces remain because the delimiter is only a comma, and TRIM was not applied. Use =ARRAYFORMULA(TRIM(SPLIT(A2, ","))), which returns red, green, blue.
Exercise
A column holds codes such as item (v2.5). You want only 2.5. Your formula =REGEXEXTRACT(A2, "(\d+.\d+)") returns 2.5 for this row but also returns 2 5 style matches on other rows such as item (v2 5). What is wrong, and what is the corrected formula? Then say how you would convert the result to a number.
Show answer
The unescaped . matches any character, so a space between digits matches too. Escape it: =REGEXEXTRACT(A2, "(\d+\.\d+)"). REGEXEXTRACT returns text, so wrap it as =VALUE(REGEXEXTRACT(A2, "(\d+\.\d+)")) to get a number.