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 analyse the data and create reports out of it. My Excel classes will help you figure out the best ways to remove unwanted spaces in your data or data strings. Read the Excel tutorial below to understand how these spaces can be removed in your Excel workbooks.
How Can You Remove Spaces in Excel Through The GUI?
Using the find and replace function helps you to remove spaces in Excel from a single sheet or from all sheets in your workbook. In this Excel class, you will help you to remove leading spaces, trailing spaces and even spaces in between strings. You also have the option to remove spaces from a selected range. The following steps will help you use Excel’s find and replace function 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.
How Can You Remove Spaces in Excel Through A Formula?
Trim is an inbuilt Excel function that helps you to remove leading and trailing spaces from your Excel data. My Excel classes will cover additional inbuilt Excel functions, but more on that later. This particular function 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.
How Can You Use Substitute to Remove Spaces in Excel?
This Excel class will teach you how to use the substitute function to remove spaces in Excel. This inbuilt Excel function helps you to remove leading spaces, trailing spaces, 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. In the place of new text, you can pass a null value.
Trim clears both leading and trailing spaces. Nevertheless, it does not allow you to clear either a 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 rtrim 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.
|Function LeadSpace(v_cell As Range)|
LeadSpace = VBA.LTrim(v_cell.Text)
|Function TrailSpace(v_cell As Range)|
TrailSpace = VBA.RTrim(v_cell.Text)
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 to remove spaces in Excel applies to Excel 2016 and might vary across versions of Excel. If you need further support, you can take one of our Excel Classes.
Excel Classes Will Increase Your Functionality with Excel
Trailing spaces and other unwanted spaces will negatively affect the utilization of certain programs and other inbuilt commands. By efficiently removing these spaces with the steps outlined in the Excel course article above and our Excel classes, you can get the most out of your spreadsheets without frequent errors.