The TRIM function in Excel removes leading spaces, extra spaces and trailing spaces. Use the SUBSTITUTE function to remove all spaces or non-breaking spaces.
1. The TRIM function below removes 2 leading spaces, 3 extra spaces and 2 trailing spaces.
Note: the TRIM function does not remove single spaces between words.
2. To get the length of a string, use the LEN function.
Explanation: the LEN function counts 2 spaces, 2 characters, 4 spaces, 3 characters and 2 spaces.
3. You can use the SUBSTITUTE function to remove all spaces.
Note: the SUBSTITUTE function substitutes spaces (second argument) with empty strings (third argument).
Text imported from other applications may contain ASCII characters. The CLEAN function removes the first 32 non-printable ASCII characters (codes 0 through 31).
4. The CLEAN function below removes a non-printable character, CHAR(7).
5. Simply combine CLEAN and TRIM to remove non-printable characters and spaces.
6. The CLEAN function removes line breaks. To enter a line break, press ALT + ENTER.
7. You can also use the SUBSTITUTE function to remove a line break, CHAR(10), and replace it with something else. For example, a comma and a space.
The CLEAN function doesn’t remove all ASCII characters. Simply use the SUBSTITUTE function to remove other ASCII characters.
8. First, find out the code number of the character.
9. Next, use SUBSTITUTE and CHAR to return the text string without this character.
10. If your text contains non-breaking spaces, CHAR(160), the TRIM function doesn’t work.
11. You can use the SUBSTITUTE function to substitute non-breaking spaces, CHAR(160) with normal spaces, CHAR(32). Don’t forget to add a TRIM function to remove the extra spaces.
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 Excel Starter 2010 More…Less
This article describes the formula syntax and usage of the TRIM function in Microsoft Excel.
Description
Removes all spaces from text except for single spaces between words. Use TRIM on text that you have received from another application that may have irregular spacing.
Important: The TRIM function was designed to trim the 7-bit ASCII space character (value 32) from text. In the Unicode character set, there is an additional space character called the nonbreaking space character that has a decimal value of 160. This character is commonly used in Web pages as the HTML entity, . By itself, the TRIM function does not remove this nonbreaking space character. For an example of how to trim both space characters from text, see Top ten ways to clean your data.
Syntax
TRIM(text)
The TRIM function syntax has the following arguments:
-
Text Required. The text from which you want spaces removed.
Example
Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet. For formulas to show results, select them, press F2, and then press Enter. If you need to, you can adjust the column widths to see all the data.
Formula |
Description |
Result |
=TRIM(» First Quarter Earnings «) |
Removes leading and trailing spaces from the text in the formula (First Quarter Earnings) |
First Quarter Earnings |
Need more help?
Maintenance Alert: Saturday, April 15th, 7:00pm-9:00pm CT. During this time, the shopping cart and information requests will be unavailable.
Categories: Advanced Excel
Tags: Excel Formula Remove Spaces
Does this sound familiar? You are searching for data that you know you’ve entered, but a query for Joe Smith simply doesn’t return any results. Or, how about – you are trying to add up two columns of numbers, but you keep getting errors.
Often, these issues are caused by extra spaces hiding before, after or between the text and numbers in your data. Especially when information has been copy/pasted into a spreadsheet, extra spaces can come along for the ride and lurk unnoticed until something goes wrong.
Excel offers several ways to help you remove spaces and clean up your data, but you will want to choose the right technique for the type of data you are working with. The following tips will describe two methods using functions and formulas to remove unwanted spaces from your data.
These steps will apply to Excel 2007-2013. Images were taken using Excel 2013 on the Windows 7 OS.
Option 1
Use the Trim Function
The TRIM function removes all spaces from text except a single space between words. Use this function when you have text data that contains hidden spaces in the cell. In our example, you’ll see that there are multiple “Joe Smiths” in our data. At a glance, several of them even look the same. However, the # of Characters column reveals that there are hidden spaces buried in the text.
Step 1
To use the TRIM function, you will need to create a temporary, or helper, column for the corrected data at the end of your spreadsheet. For the purposes of this demonstration, we’ve called the column “Trim.”
To follow using our example above, download Excel Remove Spaces Formula Template
Step 2
Click in the first cell of the Trim column. On the Formulas tab, click the Text dropdown menu in the Functions group and select TRIM.
Step 3
After the dialog box appears, click on the cell that contains the text you would like to remove spaces from to complete the function. Click OK.
Step 4
Copy the function to the remaining cells in the column to apply the function to the rest of the Name cells. You can see that the TRIM function cleaned up the extra spaces and the character counts are now all the same except for the “JoeSmith” that did not have any spaces between the first and last name. Note that TRIM will not ADD spaces.
Step 5
Now you just need to replace your original data with your clean data. To do this, select all of the cells in the Trim column, then hit CTRL+C to copy the cells to the clipboard.
Then, select the first cell in the Name column and right-click. Select Paste Values under the Paste Options heading. Now you can Delete the Trim helper column and your Name column is clean.
Option 2
Use a Formula to Remove All Spaces
If you need to prepare your data for import into a database (such as MS Access) or have a combination of number and text data that simply needs ALL spaces removed, you can use a formula instead of the TRIM function to remove ALL blanks in a cell.
Step 1
Create a temporary, or helper, column for the corrected data at the end of your spreadsheet like you did for the TRIM function. In this example the column is called No Spaces.
Step 2
Click in the first cell of the Trim column. On the Formulas tab, click the Text dropdown menu in the Functions group and select SUBSTITUTE.
Step 3
When the dialog box appears:
- Click on the first cell in the column that you need changed
- In the “Old_text” field, enter the character you want replaced separated by quotes. To remove spaces this would then be ” “.
- In the “New_text” field, enter the character you want to appear instead. Since we want NO characters to replace the space, type “”.
Step 4
Click OK to apply the formula to the cell.
Step 5
Copy the formula to the remaining cells in the helper column. Then copy the data to the data column as you did before using CTRL-C and Paste Options.
Bonus Hint!
You can also remove spaces using the Find/Replace feature in Excel. Click CTRL+F to open the Find dialog box, then click the Replace tab. Enter one space ” ” in the Find what: field and leave the Replace with: field empty to remove all spaces.
Warning: This technique will search for and replace ALL spaces in the entire sheet or selection area. Use carefully so that you don’t remove spaces you still want or need!
With these tips, you can clean up data entered from multiple sources and ensure that it is consistent and less prone to unintended duplication. Use these tips to remove spaces when you want to:
- Find unintended duplicates in your data
- Prepare someone else’s data before you import it into your own spreadsheets
- Prepare your data for export into a database
When do you find the need to find and remove unwanted spaces?
PRYOR+ 7-DAYS OF FREE TRAINING
Courses in Customer Service, Excel, HR, Leadership,
OSHA and more. No credit card. No commitment. Individuals and teams.
-
1
Highlight the range in which you want to remove all spaces. For example, if you want to remove spaces from C2 through C30, highlight those cells.
-
2
Click the Edit menu.
Advertisement
-
3
Select Find.
-
4
Select Replace…. A dialog box will appear.
-
5
Click the box beneath “Find What.”
-
6
Press the space bar on the keyboard. Make sure to press it only once.
-
7
Click Replace All. It’s the second button at the bottom of the window. The spaces are now removed from the selected cells. A pop-up will appear, letting you know how many spaces were removed.
-
8
Click OK.
Advertisement
-
1
Click the top cell in a blank column. The cell must be on the same row as the first line of data in the column with the spaces.
- For example, if you want to remove spaces from column C, and C’s first row of data is in row 2 (C2), click the second cell in your blank column (e.g. E2, F2, G2, etc).
-
2
Type =Substitute.[1]
-
3
Click the first cell in the column with spaces. For example, if you want to remove all of the spaces from the C column, click the first box (e.g. C2) in the column that isn’t the title.
- If you clicked cell C2, the formula should now look like this: =Substitute(C2 .
-
4
Type , (a comma). The cell should now look like this: =Substitute(C2,.
-
5
Type " ",. There’s a space between the two sets of quotes—this is important.
- The formula should now look like this: =Substitute(C2," ",.
-
6
Type ””). This time, there’s NO space between the sets of quotes.
- The formula should now look like this: =Substitute(C2,” “,””).
-
7
Press ↵ Enter or ⏎ Return. You will now see the contents of the selected cell (C2, in this example) without spaces in the new column.
- For example, if C2 said w ww . wikih ow .com, your new cell will say www.wikihow.com.
-
8
Click the cell with the formula you typed. The cell should now be highlighted.
-
9
Drag the fill handle down over the cells you want to fill. The data from each corresponding cell will now appear in your new column without spaces.
-
10
Copy the data from the new column to the original column. Your new space-free data is now in place.
Advertisement
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Thanks for submitting a tip for review!
About This Article
Thanks to all authors for creating a page that has been read 72,767 times.