How to Remove Spaces in Excel: A Step-by-Step Guide
Removing spaces from data in Excel can be a tedious task, especially when dealing with large datasets. However, it's a crucial step in data cleaning and preparation, especially when working with formulas, pivot tables, or data visualization. In this article, we'll show you how to remove spaces in Excel using various methods.
Why Remove Spaces in Excel?
Spaces can cause problems when working with data in Excel. For instance, they can lead to errors in formulas, make data difficult to read, and affect data sorting and filtering. Additionally, spaces can be a hindrance when trying to merge data from different sources or perform data analysis.
Method 1: Using the TRIM Function
The TRIM function is one of the most common ways to remove spaces in Excel. This function removes spaces from text strings, making it ideal for data cleaning. To use the TRIM function, follow these steps:

- Select the cell or range of cells containing the data you want to trim.
- Go to the Formula bar and type =TRIM(A1), where A1 is the cell containing the data. You can also select a range of cells by clicking and dragging your mouse.
- Press Enter to apply the TRIM function.
Alternatively, you can also use the TRIM function in a formula to remove spaces from a range of cells. For example:
=TRIM(B2:C10)
This formula removes spaces from the range B2:C10.

Method 2: Using the SUBSTITUTE Function
The SUBSTITUTE function is another effective way to remove spaces in Excel. This function replaces specific characters with other characters, making it ideal for removing spaces from data. To use the SUBSTITUTE function, follow these steps:
- Select the cell or range of cells containing the data you want to remove spaces from.
- Go to the Formula bar and type =SUBSTITUTE(A1," ",""), where A1 is the cell containing the data. You can also select a range of cells by clicking and dragging your mouse.
- Press Enter to apply the SUBSTITUTE function.
Alternatively, you can also use the SUBSTITUTE function in a formula to remove spaces from a range of cells. For example:
=SUBSTITUTE(B2:C10," ","")
This formula removes spaces from the range B2:C10.
Method 3: Using the TEXTJOIN Function (Excel 2019 and Later)
The TEXTJOIN function is a more recent addition to Excel, and it's a great way to remove spaces from data using an array formula. To use the TEXTJOIN function, follow these steps:
- Select the cell or range of cells containing the data you want to remove spaces from.
- Go to the Formula bar and type =TEXTJOIN("",TRUE,A1:A10), where A1:A10 is the range of cells containing the data. You can also select a range of cells by clicking and dragging your mouse.
- Press Ctrl+Shift+Enter to apply the TEXTJOIN function as an array formula.
Method 4: Using VBA Macro
For large datasets or complex data manipulation, using a VBA macro can be a more efficient way to remove spaces in Excel. To use a VBA macro, follow these steps:
- Open the Visual Basic Editor by pressing Alt+F11 or by navigating to Developer > Visual Basic.
- Insert a new module by clicking Insert > Module.
- Paste the following VBA code into the module:
Sub RemoveSpaces()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Cells.Replace What:=" ", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
- Close the Visual Basic Editor and save the workbook.
- Run the macro by clicking Developer > Macros and selecting RemoveSpaces.
This macro removes spaces from the entire worksheet, so be sure to adjust the code if you only want to remove spaces from a specific range of cells.
Conclusion is not necessary in this case, but the takeaway is that removing spaces in Excel is a relatively straightforward process, and the method you choose will depend on your specific needs and the size of your dataset.
Tips and Variations
When removing spaces in Excel, keep in mind the following tips and variations:
- If you want to remove spaces from a specific column or range of cells, use the TRIM or SUBSTITUTE function with the column letter or range in place of A1.
- When using the SUBSTITUTE function, make sure to use double quotes to enclose the space character, like this: =" ".
- When using the TEXTJOIN function, make sure to use the TRUE argument to preserve the original order of the cells.
- When using a VBA macro, make sure to adjust the code to match your specific needs and dataset.
Frequently Asked Questions
Here are some common questions related to removing spaces in Excel:
Q: How do I remove spaces from a specific column?
A: Use the TRIM or SUBSTITUTE function with the column letter in place of A1, like this: =TRIM(B2:B10) or =SUBSTITUTE(B2:B10," ","").
Q: How do I remove spaces from an entire worksheet?
A: Use the TEXTJOIN function with the TRUE argument, like this: =TEXTJOIN("",TRUE,A1:A10). Or, use a VBA macro like the one provided earlier.
Q: Can I remove spaces from a range of cells using the TRIM function?
A: Yes, use the TRIM function with the range of cells in place of A1, like this: =TRIM(B2:C10).
Q: How do I preserve the original order of the cells when using the TEXTJOIN function?
A: Use the TRUE argument in the TEXTJOIN function, like this: =TEXTJOIN("",TRUE,A1:A10).