Trim a String in Excel: Remove Extra Spaces Easily
In this article
To trim a string in Excel, use the =TRIM() function. This formula removes all leading and trailing spaces from a cell and reduces multiple internal spaces between words to a single space. For example, if cell A1 contains " Hello World ", the formula =TRIM(A1) will return "Hello World". This is essential for cleaning data imported from external databases or web scrapes where invisible spacing often breaks VLOOKUPs and Pivot Tables.
What You'll Need
- A computer with Microsoft Excel installed (works on all versions including Excel 365, 2021, 2019, and Excel for the Web).
- A dataset containing text strings with irregular spacing.
- An empty column adjacent to your data to house the formula results.
Step-by-Step
- Identify the target data: Locate the column containing the text you need to clean. For this guide, we will assume your messy text is in Column A, starting at cell A2.
- Create a helper column: Right-click the header of Column B (or any empty column) and select Insert. This gives you a dedicated space to perform the trimming without overwriting your original data.
- Enter the TRIM formula: Click on cell B2 and type
=TRIM(A2). Press Enter. You will see the cleaned version of the text from A2 appear in B2. - Apply the formula to the entire range: Hover your mouse over the bottom-right corner of cell B2 until the cursor turns into a small black cross (the Fill Handle). Double-click the Fill Handle or click and drag it down to the bottom of your dataset. Excel will automatically adjust the formula for each row (e.g.,
=TRIM(A3),=TRIM(A4), etc.). - Convert formulas to static values: Because the trimmed text is currently a formula, deleting Column A would cause Column B to show an error. To fix this:
- Highlight all the trimmed cells in Column B.
- Right-click and select Copy (or press
Ctrl + C). - Right-click on the same selection, and under Paste Options, select Values (the icon with "123"). This replaces the formula with the actual cleaned text.
- Clean up the sheet: Now that the cleaned data is static, you can right-click Column A and select Delete. Your cleaned data is now in its final place.
Common Mistakes
One of the most frequent mistakes users make is forgetting to Paste as Values. If you simply delete the original column after using the TRIM function, you will see a #REF! error in every cell. This happens because the TRIM formula is still looking for the original cell to pull data from; once that cell is gone, the reference is broken. Always ensure you convert your formulas to values before removing the source data.
Another common error is expecting TRIM to remove all spaces. It is important to understand that TRIM is designed to clean "whitespace," not delete every space character. If you have a string like "New York" and you want it to become "NewYork", TRIM will not help you because the single space between the words is considered necessary. For total space removal, you must use the SUBSTITUTE function.
Users also frequently try to apply TRIM directly to the cell they are editing. You cannot type =TRIM(A2) inside cell A2 itself, as this creates a circular reference. The formula must always point to a different cell than the one it resides in.
Tips
Combining TRIM with CLEAN: Sometimes, data imported from the web contains non-printable characters (like line breaks or tab characters) that the TRIM function cannot see. To handle this, nest the TRIM function inside the CLEAN function. Use the formula =TRIM(CLEAN(A2)). The CLEAN function removes the non-printable characters first, and then TRIM removes the remaining extra spaces.
Handling Non-Breaking Spaces: If you find that TRIM is not removing certain spaces, you are likely dealing with "non-breaking spaces" (often found in HTML data, represented as ). These are different from standard spaces (ASCII 32) and are ignored by the TRIM function. To fix this, use a combination of SUBSTITUTE and TRIM: =TRIM(SUBSTITUTE(A2, CHAR(160), " ")). This tells Excel to find every non-breaking space (Character 160) and replace it with a standard space, which TRIM can then easily remove.
Using Flash Fill for Simple Tasks: If you have a very small dataset and don't want to write a formula, you can use Flash Fill. Type the cleaned version of the first two cells in the adjacent column. Highlight the third cell and press Ctrl + E. Excel will attempt to recognize the pattern of removing spaces and fill the rest of the column automatically.
Troubleshooting
The formula is visible, but the result isn't: If you see =TRIM(A2) written in the cell instead of the cleaned text, your cell is likely formatted as "Text" instead of "General". To fix this, select the cell, go to the Home tab, change the Number Format dropdown to General, then double-click the cell and press Enter to re-trigger the formula.
TRIM isn't removing the spaces: If the spaces are still there after applying the formula, check if the spaces are actually spaces. As mentioned in the tips section, non-breaking spaces are the most common culprit. You can test this by using the formula =CODE(RIGHT(A2,1)) on a cell that seems to have a trailing space. If the result is 160 instead of 32, you must use the SUBSTITUTE method described above.
The data looks correct, but VLOOKUP still fails: Even after trimming, your data might have hidden characters or different casing. Ensure that both the lookup value and the table array have been trimmed. If they have, try using the =EXACT() function to compare two cells that look identical but aren't matching; if EXACT returns FALSE, there is still a hidden difference in the strings.
Frequently Asked Questions
Can TRIM remove spaces from the middle of a word?
No, TRIM only removes leading and trailing spaces and reduces multiple spaces between words to one. To remove all spaces regardless of position, use =SUBSTITUTE(A2, " ", "").
Does TRIM work on numbers?
TRIM is designed for text. If you apply it to a number, Excel will convert that number into a text string. This may cause issues if you plan to use those numbers in mathematical calculations later.
Is there a way to trim an entire column without a helper column?
Not with a standard formula. You must use a helper column or a VBA macro to overwrite the original data. Alternatively, you can use the "Find and Replace" tool (Ctrl + H) to replace double spaces with single spaces repeatedly until all are gone, though this won't handle leading/trailing spaces as effectively as TRIM.
Will TRIM remove tabs or line breaks?
No, TRIM only handles standard space characters. To remove tabs, carriage returns, and other non-printable characters, you must use the CLEAN function: =CLEAN(A2).
Can I use TRIM in a Power Query?
Yes, and it is often more efficient. In the Power Query Editor, right-click the column header, select Transform, and then click Trim. This cleans the entire column without needing formulas.