Sub topic 3
Remove extra spaces with Trim whitespace
This chapter assumes you can select ranges, use the menu bar, and write a simple formula such as =A1&B1. It also assumes you have a copy of your raw data, as covered in the previous chapter. The chapter covers the Trim whitespace menu tool: what it changes, what it leaves alone, and how to check its work.
Why extra spaces cause trouble
Whitespace is any character that shows as blank space, such as the space you type with the space bar. Extra spaces are hard to see but easy to cause. They come from copy and paste, from exports of other systems, and from people typing a space before or after a value.
Sheets compares text exactly. These three cells look the same but are three different values:
| Cell | Contents | Length |
|---|---|---|
| A2 | `Jane Doe` | 8 |
| A3 | `Jane Doe ` (trailing space) | 9 |
| A4 | `Jane Doe` (two spaces inside) | 9 |
Because they differ, these things break:
VLOOKUPandMATCHreturn#N/Awhen a key has a stray space.COUNTIFcounts "Jane Doe" and "Jane Doe " separately.- Pivot tables list the same customer in two rows.
- Duplicate removal keeps both copies, because they are not identical.
Fixing spaces first makes every later step in this tutorial more reliable. That is why trimming comes before deduplicating and splitting.
What the tool does
The tool is at Data > Data cleanup > Trim whitespace. It changes text in the selected cells in three ways:
- It removes leading spaces, which come before the first visible character.
- It removes trailing spaces, which come after the last visible character.
- It reduces any run of spaces between words to a single space.
So " Jane Doe " becomes "Jane Doe".
It does not change capitalization, spelling, or any characters other than spaces. A single space between words is kept, because that space is part of the data.
Use the tool step by step
- Make sure a backup copy of the raw data exists. Duplicate the sheet tab, or keep the original columns and work on a copy.
- Select the range to clean. To clean one column, click its letter. To clean a block, drag across it.
- Open Data > Data cleanup > Trim whitespace.
- Read the confirmation Sheets shows. It reports how many cells it changed.
- Spot-check a few cells that you know were messy.
The tool works in place. It overwrites the cells you selected, and it does not create a second column. This is the main difference from the TRIM formula, which you meet in the next chapter. If the result is wrong, press Ctrl+Z (Cmd+Z on Mac) straight away. After you have made many other edits, undo is no longer a dependable way back, so the backup matters.
Find the cells that need trimming
You do not have to trim blindly. A helper formula can show which cells are affected. Put this in B2, next to the data in A2, and fill it down:
=LEN(A2)<>LEN(TRIM(A2))It returns TRUE when trimming would shorten the text, so it flags cells with leading, trailing, or doubled spaces. Use Data > Create a filter on column B and show only TRUE to see exactly which rows are dirty.
To count them all at once, use:
=SUMPRODUCT(LEN(A2:A)<>LEN(TRIM(A2:A)))Note that this formula counts blank rows as unchanged, because an empty cell has length 0 both before and after trimming. You can run the count before and after the tool. The second count should be 0, or close to it, as explained in the next section.
What the tool does not fix
Trim whitespace handles ordinary spaces, character code 32. It does not remove every character that looks like a space. Expect these to survive:
- Non-breaking spaces, character code 160. These appear in text copied from web pages and some exports. They look like a normal space but are a different character, so trimming skips them.
- Non-printing characters, such as line-break or control characters from a database export.
- Spaces that sit inside a number-like value, such as
1 000, where the space is a separator and not padding.
If the check formula still returns TRUE after the tool has run, or if lookups still fail, a hidden character is the usual cause. You can test a suspect cell with:
=CODE(RIGHT(A2, 1))A result of 32 is a normal space. A result of 160 is a non-breaking space. The next chapters in this tutorial cover removing these characters with CLEAN and SUBSTITUTE.
The tool also leaves line breaks inside a cell alone. A cell with two lines of text stays on two lines.
Edge cases
Numbers stored as text. A cell such as " 42 " is text, not a number. After trimming it reads 42, but it may still be stored as text and may still be left-aligned. Check with =ISNUMBER(A2). If it returns FALSE, convert the value with =VALUE(A2) or =A2*1 in a helper column.
Formulas in the selection. The tool changes the values it finds in cells. If your range contains formulas, the tool is not the right choice, because trimming a formula's output does not fix the formula. Trim the source data instead, or wrap the formula in TRIM.
Dates. Real dates are stored as numbers, so there is nothing to trim. A date that was imported as text, such as " 2026-11-28", is trimmed like any other text but stays text. It needs separate conversion.
Large or whole-sheet ranges. Selecting an entire sheet works but is slower, and you may change cells you did not intend to touch, such as notes in a side column. Select only the columns that hold data you want cleaned.
Merged cells and protected ranges. The tool may fail or skip cells in protected ranges, and merged cells can give surprising selections. Unmerge or unprotect first if you see a warning.
Worked example
Suppose you imported a contact list. Column A has names:
| Row | A (raw) |
|---|---|
| 2 | ` Jane Doe` |
| 3 | `Jane Doe ` |
| 4 | `Sam Lee` |
| 5 | `Sam Lee` |
- Duplicate the sheet tab and name the copy
Contacts_raw. Work on the original tab. - In B2 enter the check formula, then fill it down:
=LEN(A2)<>LEN(TRIM(A2))Rows 2, 3, and 5 return TRUE. Row 4 returns FALSE.
- Select A2:A5 and run Data > Data cleanup > Trim whitespace.
- Look at column B. All four rows now return
FALSE, which means trimming has nothing left to shorten. - Check the effect on counts. Before trimming,
=COUNTIF(A2:A5, "Jane Doe")returns 0, because neither of the two Jane cells matched exactly. After trimming, it returns 2.
The final count shows the practical reason for the step: matching, counting, and deduplicating now see the two Jane rows as the same value.
Tool or formula?
Both approaches use the same trimming rule for ordinary spaces. They differ in how they behave.
| Question | Trim whitespace tool | `TRIM` formula |
|---|---|---|
| Where does the result go? | Over the original cells | Into a new column |
| Does it keep the original? | No, unless you made a backup | Yes |
| Does it update when data changes? | No, it runs once | Yes |
| Best for | One-off cleanup of a pasted or imported block | Repeatable cleanup of data that keeps changing |
Use the tool for a quick fix to data you will not import again. Use the formula when new rows keep arriving, or when you want the raw column preserved beside the clean one. The TRIM function is the topic of the next chapter.
Common errors
- Trimming the wrong range. Selecting only part of a column leaves the rest dirty. Check that the selection reaches the last row.
- Assuming it fixed everything. Lookups can still fail because of non-breaking spaces. Use the check formula, not the look of the cell.
- Trimming before backing up. The original spacing is gone once you have moved on.
- Expecting numbers to convert. Trimmed text that holds digits can still be text.
Exercise
Cell A2 contains the text " Ann Lee " (two spaces before, three spaces between, one space after). You run Data > Data cleanup > Trim whitespace on A2. What text is in A2 afterwards, and what does =LEN(A2)<>LEN(TRIM(A2)) return? Then, if the formula still returned TRUE, what would you suspect and what formula would help you confirm it?
Show answer
A2 becomes "Ann Lee" (7 characters): leading and trailing spaces are removed and the run of three inner spaces becomes one. The formula returns FALSE, because trimming has nothing left to remove. If it still returned TRUE, suspect a non-breaking space (character code 160) or another hidden character that the tool does not remove. Use =CODE(RIGHT(A2, 1)) to inspect the last character. A result of 160 confirms a non-breaking space, whereas 32 is an ordinary space.