Abstract

Many times, data is entered into Excel workbooks with spaces. These unwanted spaces might be at the beginning of the data or at the end or even sometimes in between strings.

 These spaces can cause issues when you try to analyze the data and create reports out of it. The following options will help you remove unwanted spaces from your Excel sheets.

Remove spaces through the GUI

Level of difficulty: LOW

How To Remove Spaces In Excel Find and Replace:

This option helps you to remove space from a single sheet or from all sheets in your workbook. This option will help you to remove leading spaces, trailing spaces and even spaces in between strings.

You also have the option to remove space from a selected range. The following steps will help you use Excel’s find and replace to remove spaces through the GUI.

Step 1: Select the range from which spaces must be removed.

Step 2: From the Home header in the Excel ribbon, select “Editing”. Then click on “Find & Select” and select “Replace”. If you are comfortable in using keyboard shortcuts, press “Ctrl+H” to open the Find & Replace wizard.

Step 3: On the wizard, under the tab “Header” you will see two text boxes. The first text box is “Find What:” and the second text box is the “Replace with:” fields. In the “Find What:” text box type a space and leave the “Replace with:” with a NULL value.

Step 4: The wizard will also have 3 drop downs. The first drop down allows the user to do the find and replace within the sheet or at the workbook level. The second drop down makes the search at Row level or at the column level. The third drop down makes Excel search on formulas or on values.

Step 5: Click on “Replace All” to remove all spaces from your data.

Remove spaces through formula

Level of difficulty: LOW

TRIM

This inbuilt excel function helps you to remove leading and trailing spaces from your Excel data. However, this will not remove spaces between strings. The only drawback with this option is, you must type and apply the formula in a separate cell and then copy it and using ‘paste special’ paste it as value.

Step 1: Use an existing empty column or create a new column and start typing the formula =TRIM(CELL_ADDRESS). Please note, you have to update the CELL_ADDRESS in this formula with the actual address of the starting cell in your Excel range.

Step 2: Press enter to see the leading and trailing spaces being removed from the data in the selected cell.

Step 3: Select the cell in which you typed the formula. Click and drag the bottom-right corner of it until you reach the last cell in the excel range.

Step 4: The new column will now contain the same data from the selected range. However, this will not contain the leading and trailing spaces. This new column will still contain the formulas within it. We should remove the formulas without affecting the displayed data.

Step 5: Select all cells in this new column, copy it and then using ‘paste special’ paste the values. You can paste data directly on your original column or in a new column.

SUBSTITUTE

This inbuilt excel function helps you to remove leading space, trailing space and spaces between your Excel data. As the name implies, this function substitutes space with the replacing character that you input in the formula. The syntax is
=SUBSTITUTE(Text, Old text, New Text)

As shown below, for the first parameter, you can pass a string or you can pass a CELL address. In the place of Old text, you have to mention an empty space and in the place of New Text, you can pass a NULL value.

 =SUBSTITUTE(A2,” “,””)
How To Remove Spaces In Excel 1

How To Remove Spaces In Excel Remove spaces using user define function

Level of difficulty: MEDIUM

TRIM clears both leading and trailing spaces. Nevertheless, it does not allow you to clear either leading space or the trailing space. However, you can achieve this using user defined functions. To build your custom functions, you would be using the function LTRIM and RTRIM which is available only with VBA. LTRIM removes the leading space and RTIM removes the trailing space.

Step 1: If you are on worksheets, press Alt+F11 to open the VBA editor.

Step 2: Right click on the VBA project at the left, select the option ‘Insert’ and then click “Module”. This will insert a new module into your VBA project.

Step 3: Copy these scripts into your module and save your workbook as a macro enabled file. The first script will create a new user defined function with which you can remove leading spaces from a string. The second script will create a new user defined function with which you can remove trailing spaces.
How To Remove Spaces In Excel 2

Step 4: You can now use these user defined functions in your Worksheet formulas or in your VBA scripts.

Step 5: Below is an example for using this user defined function in Worksheet formula

This will remove leading space from the Cell A2

Please note, this guide applies to Excel 2016 and might vary across versions of Excel.