When importing text from external sources into Excel, it's common to encounter unwanted spaces at the beginning, end, or within the text strings. These extraneous spaces can cause issues with data analysis, sorting, and formula results. Excel provides a handy solution with the TRIM function, which efficiently removes these unnecessary spaces from your data.
The TRIM function in Excel eliminates spaces from both sides of a text string and reduces multiple spaces between words to a single space. It removes the ASCII space character (32) but does not remove non-breaking spaces often found in web pages (character code 160). Here's how you can use the TRIM function to clean up your data effectively.
Using the TRIM Function to Remove Extra Spaces in Excel
The syntax of the TRIM function is straightforward:
=TRIM(text)
Here, text
can be a direct text string enclosed in quotation marks or a reference to a cell containing the text.
Consider the following worksheet where cells contain text with leading spaces, trailing spaces, double spaces, and multiple extra spaces. We'll use the TRIM function to clean these entries.
Step 1: Select the cell where you want the cleaned text to appear. For instance, choose cell B1.
Step 2: Enter the TRIM function with the cell reference of the text you want to clean. In this example, type =TRIM(A1)
in cell B1.
As a result, all leading, trailing, and extra spaces within the text in cell A1 are removed in cell B1.
You can also use the TRIM function directly on a text string by enclosing it in quotation marks. For example:
Applying the TRIM Function to Multiple Cells
To remove unwanted spaces from multiple cells in a column, you can apply the TRIM function to one cell and then extend it to the rest of the cells.
Step 1: After applying the TRIM function to the first cell (e.g., cell B1), locate the fill handle—a small green square at the bottom-right corner of the cell.
Step 2: Click and drag the fill handle down over the cells where you want to apply the function.
This action copies the TRIM function to the selected cells, cleaning the text in each corresponding cell of the original column.
Removing Leading Spaces Only Using the TRIM Function
Sometimes, you might want to remove only the leading spaces without affecting spaces between words, such as double spaces added for readability. For example, consider addresses that have double spaces between different parts but also have unwanted leading spaces.
Using the TRIM function alone would remove all extra spaces, including the double spaces between address components. To remove only the leading spaces, you can combine TRIM with other functions:
=REPLACE(A1,1,FIND(LEFT(TRIM(A1),2),A1)-1,"")
This formula works as follows:
Step 1: TRIM(A1)
removes all extra spaces from the text in cell A1.
Step 2: LEFT(TRIM(A1),2)
extracts the first two characters of the trimmed text.
Step 3: FIND(LEFT(TRIM(A1),2),A1)-1
calculates the position of the first non-space character in the original text.
Step 4: REPLACE(A1,1,...,"")
removes the leading spaces by replacing them with an empty string.
Removing Non-Breaking Spaces Using the TRIM Function
The TRIM function does not remove non-breaking spaces (character code 160), which can appear when copying text from web pages. These spaces may not be visible but can affect your data.
To remove non-breaking spaces, you can use the TRIM function in combination with the SUBSTITUTE function:
=TRIM(SUBSTITUTE(A11,CHAR(160)," "))
Here's how this formula works:
Step 1: SUBSTITUTE(A11,CHAR(160)," ")
replaces non-breaking spaces with regular spaces in the text from cell A11.
Step 2: The TRIM function then removes the extra spaces from the result.
By utilizing the TRIM function and combining it with other Excel functions, you can efficiently clean your text data, ensuring accuracy in your spreadsheets.
Member discussion