How to Remove Extra Spaces in Google Sheets: Leading, Trailing and Double Spaces, Non-Breaking Spaces, Line Breaks and Why TRIM Isn’t Working (2026)
Extra spaces are invisible in a cell but not to a formula: “ana@example.com ” with a trailing space is a different value from “ana@example.com”, so formulas such as COUNTIF and EXACT treat them as different values and duplicates go unnoticed. Google’s TRIM help page puts it plainly: spaces in front of or after text are significant when the text is used in formulas or data validation. This guide finds the stray spaces, removes the normal ones with TRIM, handles the two cases TRIM leaves behind (non-breaking spaces and line breaks), gives one formula that cleans all of it, and shows how cleaning a column changes a duplicate check.
The short answer
- Leading, trailing and double spaces:
=TRIM(A2), or select the range and choose Data › Data cleanup › Trim whitespace to clean it in place. - Non-breaking spaces (TRIM “not working”):
=TRIM(SUBSTITUTE(A2, CHAR(160), " ")) - Line breaks inside a cell:
=TRIM(SUBSTITUTE(A2, CHAR(10), " ")) - All of it at once:
=TRIM(REGEXREPLACE(SUBSTITUTE(A2, CHAR(160), " "), "\s+", " ")) - Make it permanent: copy the helper column and use Edit › Paste special › Values only over the original.
The example
A contact list with ten rows. Some names and emails have spaces before, after or between them, row 6 has non-breaking spaces, and row 8 has a line break between first and last name. Three people appear twice (rows 2 and 4, 3 and 7, 6 and 9), and Eli Moss appears twice too (rows 8 and 11).
| Row | A: Name | B: Email | What’s wrong |
|---|---|---|---|
| 2 | Ana Ruiz | ana@example.com | two leading spaces in the name, a trailing space after the email |
| 3 | Ben Cole | ben@example.com | two spaces between the names |
| 4 | Ana Ruiz | ana@example.com | clean |
| 5 | Cara Diaz | cara@example.com | a trailing space after the name |
| 6 | Dev[non-breaking space]Patel | dev@example.com[non-breaking space] | a non-breaking space in the name and after the email |
| 7 | Ben Cole | ben@example.com | a leading space before the email |
| 8 | Eli[line break]Moss | eli@example.com | a line break between the names |
| 9 | Dev Patel | dev@example.com | clean |
| 10 | Fay Lee | fay@example.com | a double space and a trailing space |
| 11 | Eli Moss | eli@example.com | clean |
The plan is helper columns next to the data: C to try TRIM, D and E for the fully cleaned name and email, and G and H to count duplicates before and after. You can delete them once the clean text is pasted back.
Step 1: find the hidden spaces
You can’t see a trailing space, but you can measure it. LEN counts all characters, even spaces and non-printing characters, so comparing the length before and after TRIM shows how many spaces TRIM would remove:
=LEN(A2)-LEN(TRIM(A2))
It returns 2 for row 2 (two leading spaces), 1 for rows 3 and 5, 2 for row 10 and 0 for the rest. Rows 6 and 8 also return 0, even though they aren’t clean: what they contain isn’t a normal space. To find out what a character is, CODE returns the numeric Unicode value of the first character of a string, so pair it with MID to pick the 4th character, the one right after “Dev” and “Eli”:
=CODE(MID(A6, 4, 1))
That returns 160, a non-breaking space. =CODE(MID(A8, 4, 1)) returns 10, a line break. A normal space is 32.
Step 2: remove leading, trailing and double spaces with TRIM
Put this in C2 and fill it down to C11:
=TRIM(A2)
Google describes TRIM as removing leading, trailing and repeated spaces, leaving just a single space between words. “ Ana Ruiz”, “Ben Cole” and “Fay Lee ” come out as “Ana Ruiz”, “Ben Cole” and “Fay Lee”:
| Row | C: =TRIM(A2) | LEN(A)−LEN(TRIM(A)) |
|---|---|---|
| 2 | Ana Ruiz | 2 |
| 3 | Ben Cole | 1 |
| 4 | Ana Ruiz | 0 |
| 5 | Cara Diaz | 1 |
| 6 | Dev[non-breaking space]Patel | 0 |
| 7 | Ben Cole | 0 |
| 8 | Eli[line break]Moss | 0 |
| 9 | Dev Patel | 0 |
| 10 | Fay Lee | 2 |
| 11 | Eli Moss | 0 |
Rows 6 and 8 come back unchanged. Those are the two problems TRIM doesn’t fix, covered in the next two steps.
Trim whitespace: the same fix without a formula
To clean cells in place, select the range and choose Data › Data cleanup › Trim whitespace. Google’s data cleanup help page says it removes extra leading, trailing or excessive spaces in the selected range. It changes the cells themselves, so duplicate the tab first if you want to keep the original.
Step 3: non-breaking spaces, CHAR(160)
This is the space TRIM leaves behind. Both the TRIM page and the Trim whitespace instructions note that a non-breaking space (the of web pages) will not be trimmed. It looks like a space but it is character 160, not 32. Turn it into a normal space with SUBSTITUTE and CHAR, then trim:
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))
Row 6 becomes “Dev Patel”. SUBSTITUTE replaces every occurrence by default, so a cell with several non-breaking spaces is fixed in one pass.
Step 4: line breaks and other non-printing characters
A line break inside a cell is character 10. TRIM only deals with spaces, so replace the line break with a space first:
=TRIM(SUBSTITUTE(A2, CHAR(10), " "))
Row 8 becomes “Eli Moss”. You may see CLEAN suggested for this. It returns the text with non-printable ASCII characters removed, and a line break is one of them, but it deletes the character without putting anything in its place: =CLEAN(A8) gives “EliMoss”. Google also notes that CLEAN only removes non-printable ASCII characters, so it doesn’t touch a non-breaking space (character 160 is outside ASCII). Use SUBSTITUTE when the character sits between two words.
Step 5: one formula for all of it
To clean spaces, non-breaking spaces, tabs and line breaks in one go, put this in D2 and fill down to D11 (and the same with B2 in E2 for the emails):
=TRIM(REGEXREPLACE(SUBSTITUTE(A2, CHAR(160), " "), "\s+", " "))
Read it from the inside out. SUBSTITUTE turns non-breaking spaces into normal spaces. REGEXREPLACE then replaces every match of the regular expression \s+ with one space; Google Sheets uses RE2 syntax, where \s means a space, tab, line feed, form feed or carriage return, and + means one or more of them in a row. TRIM finally removes the space left at either end. Every name in column D and every email in column E comes out clean:
| Row | D: clean name | E: clean email | G: =COUNTIF($B$2:$B$11, B2) | H: =COUNTIF($E$2:$E$11, E2) |
|---|---|---|---|---|
| 2 | Ana Ruiz | ana@example.com | 1 | 2 |
| 3 | Ben Cole | ben@example.com | 1 | 2 |
| 4 | Ana Ruiz | ana@example.com | 1 | 2 |
| 5 | Cara Diaz | cara@example.com | 1 | 1 |
| 6 | Dev Patel | dev@example.com | 1 | 2 |
| 7 | Ben Cole | ben@example.com | 1 | 2 |
| 8 | Eli Moss | eli@example.com | 2 | 2 |
| 9 | Dev Patel | dev@example.com | 1 | 2 |
| 10 | Fay Lee | fay@example.com | 1 | 1 |
| 11 | Eli Moss | eli@example.com | 2 | 2 |
REGEXREPLACE only works with text, so use this on text columns such as names, emails and notes, not on a column of numbers. To cover the whole column with a single formula in the first cell of an empty column, use ARRAYFORMULA; blank rows stay blank:
=ARRAYFORMULA(IF(A2:A="", "", TRIM(REGEXREPLACE(SUBSTITUTE(A2:A, CHAR(160), " "), "\s+", " "))))
To check which rows the formula actually changed, =EXACT(A2, D2) returns FALSE where the cleaned name differs from the original. EXACT requires identical spaces and hidden characters, so here it is TRUE only for rows 4, 7, 9 and 11.
Step 6: replace the original text
The helper columns are formulas that read from A and B, so deleting the originals would break them. To make the change permanent:
- Right-click the sheet tab and choose Duplicate, so you have a copy to go back to.
- Select D2:D11 and copy it.
- Click A2 and choose Edit › Paste special › Values only.
- Do the same for E2:E11 into B2, then delete the helper columns.
Line breaks without a formula: Find and replace
To replace line breaks in place, choose Edit › Find and replace, type \n next to “Find” and a single space next to “Replace with”, tick Search using regular expressions (see Google’s find and replace help page) and click Replace all. Then run Data › Data cleanup › Trim whitespace on the range to tidy up any doubled spaces.
Extra spaces and duplicates
The example has four people entered twice, but on the raw emails in column B a duplicate count finds only one of them:
=COUNTIF($B$2:$B$11, B2)in G2, filled down, returns 2 only for Eli Moss (rows 8 and 11), whose emails happen to be clean. Google’s COUNTIF help page says each cell is checked against the criterion for equality, and “ana@example.com ” with a trailing space is not equal to “ana@example.com”.- On the cleaned emails,
=COUNTIF($E$2:$E$11, E2)in H2 returns 2 for all eight rows that belong to a pair: rows 2 and 4, 3 and 7, 6 and 9, and 8 and 11.
So clean the spaces before you look for repeats. With clean emails in column E, this flags them:
=IF(COUNTIF($E$2:$E$11, E2)>1, "Duplicate email", "")
To delete the extra rows, first paste the values back as in Step 6, then select the table and choose Data › Data cleanup › Remove duplicates, tick “Data has header row” and only the Email column. It removes 4 rows (rows 4, 7, 9 and 11) and keeps the first row of each pair. If the two copies hold different details, merge the rows instead, and see duplicates that don’t match exactly for case, Gmail dots and typos.
Common problems
- TRIM didn’t remove a space. It is probably a non-breaking space, which TRIM leaves alone. Check with
=CODE(MID(A2, 4, 1))(adjust the position), then use=TRIM(SUBSTITUTE(A2, CHAR(160), " ")). - The line break is still there after TRIM. TRIM only removes spaces. Use
=TRIM(SUBSTITUTE(A2, CHAR(10), " ")). - Two words got glued together. CLEAN removed a line break without adding a space. Use SUBSTITUTE with CHAR(10) instead.
- REGEXREPLACE shows an error. The cell holds a number, and REGEXREPLACE only works with text. Use TRIM on number columns, or leave them out.
- The helper column turned into errors after I deleted the original. It still pointed at the original cells. Undo, paste the helper column as values first (Step 6), then delete.
- The ARRAYFORMULA shows an error. Something is typed in the column below it. Clear those cells so the results can fill the column.
FAQ
How do I remove extra spaces in Google Sheets?
Use =TRIM(A2) in a helper column. TRIM removes leading, trailing and repeated spaces and leaves one space between words. Then copy the results and use Edit › Paste special › Values only over the original. To clean a range in place without formulas, select it and choose Data › Data cleanup › Trim whitespace.
Why is TRIM not working in Google Sheets?
If spaces are left after TRIM, they are probably non-breaking spaces (character 160). Google’s TRIM help page says non-breaking spaces will not be trimmed, and the same goes for Data › Data cleanup › Trim whitespace. Turn them into normal spaces first: =TRIM(SUBSTITUTE(A2, CHAR(160), " ")).
How do I remove line breaks inside a cell in Google Sheets?
Replace the line break character, CHAR(10), with a space and trim the result: =TRIM(SUBSTITUTE(A2, CHAR(10), " ")). CLEAN also removes line breaks, but it removes them without leaving a space, so "Eli" and "Moss" on two lines become "EliMoss".
Is there one formula that removes all extra whitespace?
=TRIM(REGEXREPLACE(SUBSTITUTE(A2, CHAR(160), " "), "\s+", " ")) turns non-breaking spaces into normal spaces, turns every run of spaces, tabs and line breaks into one space, then trims the ends. REGEXREPLACE only works with text, so use it on text columns such as names and emails.
How can I tell if a cell has hidden spaces?
Compare lengths: =LEN(A2)-LEN(TRIM(A2)) returns how many spaces TRIM would remove, and LEN counts every character, including spaces and non-printing characters. To identify a character, =CODE(MID(A2, 4, 1)) returns the code of the 4th character: 32 is a normal space, 160 a non-breaking space and 10 a line break.
Do extra spaces stop Google Sheets from finding duplicates?
Yes. COUNTIF checks each cell for equality, so "ana@example.com " with a trailing space and "ana@example.com" are counted as different values, and EXACT requires identical spaces and hidden characters. Clean the column first, then count or remove duplicates.
Install free from Google Workspace MarketplaceSee SheetDupes
Want to test it first? Try it on sample data (makes a copy of a 12-row example sheet in your Drive).
Related guides
- Duplicates That Don’t Match Exactly in Google Sheets: Case, Extra Spaces, Gmail Dots, Phone Formats and Typos (2026)
- How to Change Text Case in Google Sheets: UPPERCASE, lowercase, Proper Case Names, Sentence Case and Duplicates That Differ Only in Case (2026)
- How to Format Phone Numbers in Google Sheets: Strip Dashes and Spaces, Show (555) 010-1234, Handle +1 and Extensions, Then Find Duplicate Contacts (2026)
- UNIQUE vs Remove Duplicates in Google Sheets: Live or Static, Letter Case, Spaces, Columns and Headers (2026)
- How to Find and Remove Duplicates in Google Sheets: Highlight, Count, Delete and Keep the Original (2026)
- How to Highlight Duplicates in Google Sheets: Conditional Formatting Formulas for One Column, Whole Rows, Repeats Only and Two Sheets (2026)
- How to Compare Two Columns or Two Sheets in Google Sheets to Find Matches and Duplicates (2026)
- How to Remove Duplicates in Google Sheets but Keep the Latest, First or Highest Row per Key (2026)
- How to Prevent Duplicate Entries in Google Sheets: Data Validation Rules, Warnings and a Small Script (2026)
- How to Count Duplicates in Google Sheets: per Value, Extra Rows, Unique Counts and Why the Numbers Disagree (2026)
- How to Combine Two Lists or Several Tabs in Google Sheets Without Duplicates: VSTACK, UNIQUE, One Row per Key and Other Files (2026)
- How to Merge Duplicate Rows in Google Sheets: Sum the Numbers, Join the Notes and Fill the Blanks (2026)
- How to Remove Duplicates Within a Cell in Google Sheets: Comma-Separated Lists, Tags, Line Breaks and Letter Case (2026)
- How to Show Only the Duplicates in Google Sheets: Filter Duplicate Rows, List Repeated Values and Find Rows That Appear Once (2026)
- How to Find Duplicates Across Multiple Columns in Google Sheets: Rows That Match on Two or More Columns, and Values Repeated Anywhere in a Range (2026)
- How to Remove Duplicates Automatically in Google Sheets: Apps Script on a Daily Timer, on Form Submit or From a Menu (2026)
- How to Find Duplicate Transactions in Google Sheets: Double-Imported Rows, Invoices Paid Twice and Same-Amount Charges Within a Few Days (2026)
- How to Find Duplicates in Google Sheets but Ignore Blank Cells: Highlight, Count, List and Remove Without Empty Rows Getting in the Way (2026)