Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Strip Function: How to Remove Unwanted Spaces

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To strip unwanted spaces from a cell in Excel, use the TRIM function. This function removes all leading and trailing spaces and reduces multiple consecutive spaces between words to a single space, ensuring your data is clean for sorting, searching, and VLOOKUP functions.

What You'll Need

  • A computer with Microsoft Excel installed (any version) or access to Excel for the Web.
  • A dataset containing text strings with irregular spacing, such as data imported from a database, a website, or a CSV file.
  • An empty helper column adjacent to your data to house the cleaned results.

Step-by-Step

  1. Identify the Target Data: Locate the column containing the text you want to clean. For example, if your messy data is in Column A, starting at cell A2, this will be your reference point.
  2. Create a Helper Column: Right-click the header of the column to the right of your data (e.g., Column B) and select Insert. This provides a clean space to output the stripped text without overwriting your original source data.
  3. Enter the TRIM Formula: Click on the first empty cell of your helper column (B2). Type the following formula: =TRIM(A2). Press Enter. Excel will immediately remove the leading, trailing, and redundant internal spaces from the text in cell A2 and display the result in B2.
  4. Apply the Formula to the Entire Range: To clean the rest of the list, 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 end of your dataset.
  5. Convert Formulas to Static Values: Because the helper column currently contains formulas, the data will disappear if you delete Column A. To fix this, select all the cleaned cells in Column B. Right-click and select Copy. Right-click the same selection again and, under Paste Options, select Values (the icon with the numbers '123').
  6. Clean Up the Sheet: Now that the cleaned data is saved as static text, you can safely right-click Column A and select Delete. You may then rename Column B to reflect the cleaned data.

Common Mistakes

One of the most frequent mistakes users make is assuming the TRIM function removes all types of spaces. In reality, TRIM only removes the standard space character (ASCII 32). If you have imported data from the web, you may encounter "non-breaking spaces" (ASCII 160), which TRIM ignores completely. If your formula seems to be doing nothing, you are likely dealing with these invisible characters.

Another common error is forgetting to convert the formula results to values. Users often delete the original "dirty" column immediately after applying the TRIM function. Because the TRIM formula relies on a cell reference (like A2), deleting A2 causes the formula in B2 to return a #REF! error, effectively erasing the cleaned data.

Some users attempt to use TRIM inside a cell that is formatted as "Text" rather than "General." If you type =TRIM(A2) and the cell simply displays the text of the formula instead of the result, it is because the cell formatting is preventing Excel from executing the calculation.

Tips

For more advanced cleaning, you can nest the TRIM function inside other functions. For example, if you want to ensure the first letter of every word is capitalized after stripping the spaces, use =PROPER(TRIM(A2)). This is particularly useful for cleaning name lists where the original input was inconsistent.

If you are dealing with the non-breaking spaces mentioned earlier, you can combine TRIM with the SUBSTITUTE function. Use the formula =TRIM(SUBSTITUTE(A2, CHAR(160), " ")). This tells Excel to find every non-breaking space (CHAR 160), replace it with a standard space, and then apply the TRIM function to strip the remaining redundancies.

To remove every single space in a cell (including the spaces between words), TRIM is not the right tool. Instead, use the SUBSTITUTE function: =SUBSTITUTE(A2, " ", ""). This is ideal for cleaning phone numbers, account IDs, or SKU codes where no spaces should exist at all.

Troubleshooting

If the TRIM function is not removing spaces, first check if the characters are actually spaces. A quick way to test this is to use the LEN function. Type =LEN(A2) in a cell. If the cell looks like it has 5 characters but LEN returns 7, there are hidden characters. If TRIM doesn't change that number, you are likely dealing with non-printing characters or non-breaking spaces.

If you see a #VALUE! error, ensure that the cell reference in your formula actually contains text or a number. While TRIM usually handles numbers by converting them to text, extreme formatting issues or linked external workbooks that are closed can sometimes cause calculation errors.

If the formula is not updating automatically when you change the source data, check your Calculation Options. Go to the Formulas tab, click Calculation Options, and ensure Automatic is selected. If it is set to Manual, you will need to press F9 to refresh the TRIM results.

Frequently Asked Questions

Can TRIM remove line breaks within a cell?

No, the TRIM function only removes spaces. To remove line breaks (carriage returns), you must use the SUBSTITUTE function with the CHAR function, specifically =SUBSTITUTE(A2, CHAR(10), "") for Windows or =SUBSTITUTE(A2, CHAR(13), "") for older Mac versions.

Does TRIM work on numbers?

Yes, but it converts the number into a text string. If you need the result to remain a number for mathematical calculations, wrap the formula in the VALUE function: =VALUE(TRIM(A2)).

Is there a way to strip spaces without using a formula?

Yes, you can use the Find and Replace tool. Press Ctrl + H, type a single space in the "Find what" box, and leave the "Replace with" box empty. Note that this removes all spaces, including those between words, so it is only recommended for IDs or codes.

Can I use TRIM across multiple cells at once?

You cannot apply a single formula to a range of cells simultaneously in a way that modifies the original cells. You must apply the formula to one cell and then drag the fill handle down, or use an Array Formula (Dynamic Array) in newer versions of Excel by selecting the entire range in the formula, e.g., =TRIM(A2:A100).

Why does my data still look messy after using TRIM?

This usually happens because of "ghost" characters like tabs or non-breaking spaces. Use the =CLEAN(TRIM(A2)) combination. The CLEAN function removes the first 32 non-printing characters in the ASCII code, which often solves the issue of stubborn gaps that TRIM cannot see.