How to Clean Spaces in Excel: Remove Extra Gaps
In this article
To clean spaces in Excel, use TRIM to collapse extra spaces between words and remove leading and trailing spaces, and use SUBSTITUTE to delete every space or a specific character like a non-breaking space. Wrap either formula around your original value, then copy the result and paste it back as values so the formulas can be removed. This matters because stray spaces break lookups, filters, sorting and duplicate checks: "Apple" and "Apple " look identical on screen but Excel treats them as different values.
What You'll Need
- Excel on Windows, Mac or the web, or Google Sheets (the formulas below work in both).
- A backup copy of your sheet, or at least a free column next to your data, so you can compare before and after.
- An empty helper column if you want to keep the original values while you test.
Step-by-Step
- Spot the problem first. Select the column, then use Home > Find & Select > Find (or press Ctrl+F on Windows, Command+F on Mac). Type a space and search. If Excel finds matches at the start or end of cells, those are the spaces you need to remove. You can also check with
=LEN(A2)next to the value: a name that should be 5 characters but returns 7 has two hidden spaces. - Insert a helper column. Right-click the column header to the right of your data and choose Insert. Give it a name like "Clean" so you know what it holds. Working in a helper column means your original data stays intact until you have checked the result.
- Use TRIM for normal spaces. In the first cell of the helper column, type
=TRIM(A2)where A2 is the first cell with the messy text. Press Enter. TRIM removes leading and trailing spaces and reduces runs of multiple spaces between words to a single space. It does not remove non-breaking spaces (character 160), which often arrive when text is copied from websites or exported from other systems. - Fill the formula down. Click the small square at the bottom-right of the cell (the fill handle) and drag down, or double-click it to fill to the end of the adjacent data. Alternatively, select the range and press Ctrl+D on Windows or Command+D on Mac.
- Remove non-breaking spaces if TRIM is not enough. In a second helper column, use
=TRIM(SUBSTITUTE(A2,CHAR(160)," ")). This swaps each non-breaking space for a normal space, then TRIM tidies up. If you want to strip every space entirely (useful for codes and phone numbers), use=SUBSTITUTE(A2," ",""). - Compare before and after. Put
=LEN(A2)and=LEN(B2)side by side. The clean column should be the same length or shorter. If a value still looks wrong, check for other invisible characters such as tabs with=CODE(MID(A2,1,1))or use Find & Select to search for them. - Convert formulas to values. Select the clean column, press Ctrl+C (Command+C on Mac), then right-click the same range and choose Paste Special > Values. This replaces the formulas with plain text so they do not break when you delete the original column.
- Replace the original data. Copy the clean values, right-click the first cell of the original column and choose Paste Special > Values. Delete the helper columns. Your data is now clean and the formulas are gone.
- Clean an entire sheet at once. If many columns are affected, select the used range, run Home > Find & Select > Replace, put a space in "Find what" and leave "Replace with" empty, then choose Replace All. This deletes every space, including the ones between words, so only do it when you want that result, for example on ID codes.
Common Mistakes
- Pasting formulas over the original data. If you paste the formula column directly onto the source column, Excel shows errors or overwrites values. Always convert to values first.
- Assuming TRIM removes everything. TRIM handles ordinary spaces only. Non-breaking spaces and tabs survive and keep breaking lookups. Test with LEN before you declare the job done.
- Deleting the original before checking. Keep the source column until you have compared lengths and spot-checked several rows.
- Using Find and Replace on the whole sheet carelessly. Replacing all spaces with nothing joins words together ("New York" becomes "NewYork"). Use TRIM when you want to keep single spaces between words.
- Forgetting about trailing spaces in headers. A header like "Name " will not match a lookup or a pivot field reference. Clean headers as well as data.
Tips
- Use Flash Fill as a quick alternative: type the cleaned version next to the first value, press Enter, then press Ctrl+E on Windows or Command+E on Mac. Check the results, because Flash Fill guesses the pattern and can be wrong on inconsistent data.
- Power Query is the cleaner long-term option for repeated imports. Load the table with Data > From Table/Range, then use Transform > Format > Trim and Transform > Format > Clean to remove spaces and non-printing characters, and refresh whenever new data arrives.
- Clean your data before building pivot tables, XLOOKUP or VLOOKUP formulas, and duplicate checks. Fixing spaces afterwards means rebuilding those objects.
- If numbers are stored as text with spaces, TRIM alone will not convert them. After trimming, use Data > Text to Columns > Finish, or multiply by 1, to turn them into real numbers.
- Work on a copy when the sheet feeds a dashboard or is shared with others, so a bad paste cannot damage the live version.
Troubleshooting
- TRIM returns the same length. The hidden character is probably a non-breaking space. Use the SUBSTITUTE and CHAR(160) formula above.
- The formula shows #NAME?. Check the function name is spelled correctly and that you are using a comma or semicolon separator matching your regional settings. Some locales use semicolons between arguments.
- Values still will not match after cleaning. Compare with
=EXACT(A2,B2), which returns TRUE only when the text is identical, including case. Also check for tabs, line breaks and different letter cases. - Paste Special > Values is greyed out. Make sure you copied a range, not a single cell in edit mode, and that the sheet is not protected. Unprotect it via Review > Unprotect Sheet if you have the password.
- You pasted over data by mistake. Press Ctrl+Z (Command+Z on Mac) immediately. If that does not restore it, close without saving and reopen the backup copy.
Frequently Asked Questions
Does TRIM remove all spaces in Excel?
No. TRIM removes leading and trailing spaces and collapses repeated spaces between words to one. It leaves a single space between words and does not remove non-breaking spaces. Use SUBSTITUTE if you want every space gone.
How do I remove spaces from an entire column at once?
Add a helper column with =TRIM(A2), fill it down, copy the results, then use Paste Special > Values over the original column. Do not paste the formula itself over the source data.
Why does my lookup fail even after cleaning?
Check for non-breaking spaces, tabs, line breaks and case differences. Use =EXACT to compare two cells and =LEN to confirm lengths match. Also make sure both ranges are formatted the same way, text versus number.
Can I clean spaces without formulas?
Yes. Find & Select > Replace can delete spaces, and Flash Fill can copy a cleaned pattern. Both are faster but less predictable than TRIM, so check the results row by row.
Does this work in Google Sheets?
Yes. TRIM, SUBSTITUTE and CHAR work the same way in Google Sheets, and Paste Special > Values only is available there too.
Will cleaning spaces change my numbers?
It can. If a number is stored as text with spaces, removing the spaces may leave it as text. Convert it afterwards with Text to Columns or by multiplying by 1 so calculations work.