7 Ways To Remove Spaces in Microsoft Excel After Text

If you want to learn how to remove spaces in Excel after text along with some neat tricks and tips, follow along with the proven approaches illustrated in this effortless Microsoft Excel tutorial.

Often you get raw data from data repositories or CRM software that contains inconsistencies in text formatting, alignment, etc. The most common problem is unnecessary spaces and hidden spaces in text entries. These irregularly formatted texts interfere with search and filtering, various Excel functions, Excel automation, and so on. Not to mention the overall file size sometimes becomes unnecessarily large if these redundant spaces aren’t handled at the data extraction level.

When you know how to remove spaces after text in Excel, you can address all the above issues and more that could arise due to redundant spaces.

I’ve tried and tested multiple approaches to shred spaces after text and have found that the following methods are the most intuitive and easy to follow. Let’s begin!

📒 Read More: 9 Ways to Reduce Excel File Size

Using the Flash Fill Command

Flash Fill offers an easy way to remove spaces after texts in columns of text. You can show Excel how to modify the text strings by editing a few cells in a column. Excel will learn the pattern and apply it in the rest of the column. The method involves using a helper column in your dataset.

Sample dataset 1

In the above dataset, there are many unnecessary spaces after texts. Let me show you how to fix those using Flash Fill below.

Helper column and edit few cells
Helper column and edit few cells

Create a helper column to the right of the Employee Name column. Let’s say it’s the Helper Column 1.

In Helper Column 1, type in the employee names in the correct format for the first two to three cells.

Apply Flash Fill command
Apply Flash Fill command

now, click the column header text, and press Ctrl + E on the keyboard.

If Excel recognizes the editing pattern, it’ll instantly fill up the rest of the cells in column B with reference data from column A. Column B now contains texts without spaces after the last word or letter.

Select the content of the Helper Column 1, press Ctrl + C, select the first cell in column A, and press Ctrl + V to transfer edited data.

Delete helper column
Delete helper column

Delete the Helper Column 1.

Helper column and edit a few first cells
Helper column and edit a few first cells

Repeat the same steps for the Department column, like creating the Helper Column 2 on the right.

Second helper column
Second helper column

Edit a few leading cells and use the Flash Fill command.

Delete redundant column
Delete redundant column

Delete the Helper Column 2 once its utility is complete.

The Flash Fill command is available in the following Excel editions:

  • Excel for the web
  • Excel 2016 or later for Windows
  • Excel 2016 or newer for Mac

You can’t try out this method if you’re not using any of the above Excel versions.

Using the Find and Replace Tool

The Find and Replace tool is suitable for only trailing spaces. If there are spaces between texts, those will be replaced too.

Using Find and Replace
Using Find and Replace

To use this method, select the input dataset, and press the Ctrl + H keys.

In the Find what field, type a space.

Keep the Replace with field empty.

Click the Replace All button.

Remove spaces after text using Find and Replace
Remove spaces after text using Find and Replace

Excel will delete all spaces including trailing ones. You’ll also get a confirmation message for how many characters were changed.

Using the CLEAN Function

Often, imported raw data might contain nonprintable characters. These characters don’t show up on normal cell view. Also, printers might not be able to print these characters or print incorrect values. You can only visualize these characters by selecting a cell and looking at the formula bar.

Nonprintable trailing space between words
Nonprintable trailing space between words

For instance, in the above dataset, there are nonprintable characters in all the cells through A2:A5. Though the cells don’t show these symbols, you can see large gaps between the first and last names.

Use CLEAN function
Use CLEAN function

So, if there are trailing spaces in your dataset caused by nonprintable characters use the CLEAN function.

Select a blank cell to the right of the input dataset. In the current exercise, it’s B2.

In B2, enter the following formula and hit Enter:

=CLEAN(A2)
Removed nonprintable characters
Removed nonprintable characters

Excel will remove all undesirable characters from the selected cell.

Using Fill Handle to copy and paste formula
Using Fill Handle to copy and paste formula

Now, select B2 and drag down the fill handle in column B until there is data in the adjacent cells of column A.

You’ll instantly remove the trailing nonprintable spaces from the input dataset.

Paste Special dialog box
Paste Special dialog box

To get text output from the calculated column B, copy the column using Ctrl + C and paste that into any free column by pressing Ctrl + Alt + V.

You’ll see the Paste Special dialog box. Select Values there and click OK.

Excel will paste the calculated values as text in the destination column.

You can now move this dataset as per your preference.

Nonprintable characters removed
Nonprintable characters removed

If you select a cell, you’ll no longer see the unprintable space character.

Microsoft has introduced the CLEAN function from Excel 2013. So, you won’t find the function if you’re using Excel 2010 or earlier editions.

Using the Excel TRIM Function

The TRIM function is the default Excel function for shredding all unnecessary spaces. It works on spaces before and after texts. Also, it won’t delete the standard single space between two words.

Using TRIM
Using TRIM

To use a TRIM function, highlight an empty cell adjacent to the source dataset. For instance, B2 in this tutorial.

Enter the following formula in the cell and hit Enter.

=TRIM(A2)

In the above formula, A2 is the source text.

Excel will remove trailing spaces from the selected cell.

Using fill handle to replicate formula
Using fill handle to replicate formula

Click on the cell again and drag down the fill handle to apply the same formula to the rest of the dataset.

Copy the source data
Copy the source data

The data you’ve got so far is in formula form. Copy the content of column B by pressing Ctrl + C.

Using Paste Special Values
Using Paste Special Values

Now, select the first cell of the source column, like column A, and press Ctrl + Alt + V to use Paste Special.

In the Paste Special dialog box, select Values, and click OK.

Excel will convert the calculated data into text values and paste those into column A.

Removing a column
Removing a column

You can now delete column B to reduce clutter.

The only downside of this function is it’s only available since Excel 2016. If you’re working on an earlier version, you won’t find it.

Using the Data Validation Tool

You can prevent users from entering redundant trailing spaces in texts in Excel using a Data Validation rule. So, you no longer need to remove unnecessary spaces after texts.

To practice this method, navigate to the worksheet where you’re creating a data entry form.

Select the cell where data entry operators might include texts with unnecessary trailing space. In this exercise, it’s A2.

Data Validation command
Data Validation command

Click on the Data Validation command in the Data Tools block of the Excel Data tab.

Configuring Settings of Data Validation
Configuring Settings of Data Validation

The Data Validation dialog box will open. Click on the Settings tab and select Custom in the Allow field.

The Formula field will show up below the Data field.

Inside the Formula field, enter the following formula:

=TRIM(A2)=A2
Error Alert tab
Error Alert tab

Now, navigate to the Error tab and enter preferred warning messages in the Title and Error message field.

Click OK to save the Error message.

Copy a Data Validation
Copy a Data Validation

Now, copy A2 using Ctrl + C.

Paste Data Validation
Paste Data Validation

Select the empty table or cell range where you want to apply this Data Validation rule. Press Ctrl + V to paste the rule.

If you try to add any extra space after a text in any of these cells, you’ll get an error message as shown in the above screenshot.

Using Power Query

If you use Power Query to connect, transform, combine, and shape datasets in Excel, you can also use the tool to remove spaces after texts. It has a built-in Trim function in the Transform tab that removes trailing spaces as well as redundant spaces from a column of text data.

From Azure SQL Database
From Azure SQL Database

To import an external dataset to Power Query Editor, click on the Get Data command in the Data tab of the Excel ribbon menu.

Then, hover the cursor over an appropriate option, like From Database, From Azure, From Power Platform, and so on.

An overflow menu will show up. From this context menu, choose the final data connector, like From Azure SQL Database, if you’ve selected Azure in the previous context menu.

Follow the on-screen instructions to import your data from thereon.

From Table Range
From Table Range

For the present tutorial, I’ve imported a worksheet dataset by using the From Table/Range command in the Data tab.

Create Table
Create Table

When you do this, don’t forget to click OK on the Create Table dialog box to see your dataset in Power Query Editor.

By Delimiter
By Delimiter

Now that your source dataset is in Power Query, select the column in the editor interface by clicking on the column header.

Click on the Split Column command and choose By Delimiter in the drop-down menu.

Split Column by Delimiter
Split Column by Delimiter

You’ll see the Split Column by Delimiter wizard. There, select Space as the delimiter and the Left-most delimiter option as the Split at setting. Click OK to apply these changes.

Power Query will split the source data column into two.

The first column should have no trailing or leading spaces.

Trimming in Power Query
Trimming in Power Query

Inconsistent spaces should be in the second column. Select that, go to the Transform tab, and click on the Text Column drop-down menu.

You should see the Format option there. Click on that and select Trim from the drop-down menu.

Power Query should apply the TRIM function on the selected column to delete all trailing and leading spaces.

Two columns
Two columns

By now, you’ve got two columns of text with no trailing spaces.

Right click for context menu
Right-click for context menu

Select these columns, right-click, and choose Merge Columns from the context menu.

Merge Columns
Merge Columns

The Merge Columns wizard will show up.

Choose Space as the Separator and enter a preferred name in the New column name field. Click OK to merge columns.

Removed spaces after text using Power Query
Removed spaces after text using Power Query

Power Query will combine the separate columns into one.

Close & Load To
Close & Load To

Go to the File tab and click on the Close & Load To button.

Import Data
Import Data

You’ll see the Import Data dialog. Click on the Existing worksheet option and highlight a cell on the worksheet where you want to load datasets from Power Query.

Click OK to complete the data import process from Power Query to the Excel worksheet.

Delete columns
Delete columns

You can delete the old dataset table.

Remove spaces Power Query
Remove spaces Power Query

Congratulations! You’ve successfully learned how to remove spaces in Excel after text using Power Query.

Using Excel VBA

The Excel VBA-based automation to remove spaces in Excel after text is the easiest method you can learn. A macro created using a VBA script has the ability to guide you through visual queues and no need to code complex Excel automation programs.

If you already know Excel VBA, you can get started with the script later in this section. However, if you’re new to this feature, go through this tutorial first:

📒 Read More: How To Use The VBA Code You Find Online

Use the following script to create a VBA macro in your Excel worksheet:

VBA script 1
Sub RemoveSpaces()
    Dim rng As Range
    Dim cell As Range
    Dim inputRange As String
    
    ' Show input box to select the range
    On Error Resume Next
    Set rng = Application.InputBox("Select the range of cells:", Type:=8)
    On Error GoTo 0
    
    If rng Is Nothing Then
        MsgBox "No range selected. Exiting..."
        Exit Sub
    End If
    
    ' Loop through each cell in the selected range
    For Each cell In rng
        ' Remove leading and trailing spaces
        cell.Value = Trim(cell.Value)
        ' Remove multiple spaces between words
        cell.Value = WorksheetFunction.Trim(cell.Value)
    Next cell
    
    ' Show completion message
    MsgBox "Spaces removed successfully!"
End Sub
Macro dialog box
Macro dialog box

When ready, press the Alt + F8 keys on the keyboard together to launch the Macro dialog box.

Select the RemoveSpaces macro in the dialog and hit Run to execute that.

Input box for dataset
Input box for dataset

As you run the macro, Excel will show a dialog box so you can select one or many cell ranges for processing.

Confirmation box
Confirmation box

Once you hit OK in the input dialog box, Excel instantly removes all trailing spaces from your text dataset.

📝 Note: Any changes made in an Excel worksheet using a VBA macro remove all Excel undo memories. So, you won’t be able to revert the changes you make using Excel VBA. Run a script after being absolutely sure that you can let go of the undo features.

Conclusions

So far, you’ve learned how to remove spaces in Excel after text using different Excel user interface tools, like Flash Fill and Find and Replace.

You’ve also seen how you can write manual Excel functions, like CLEAN and TRIM to replace trailing spaces with nothing.

Moreover, you’ve learned how to prevent trailing spaces when entering data manually in Excel using the Data Validation tool.

Finally, you’ve learned two advanced methods, like Power Query and Excel VBA that enable you to automate the process to a great extent.

Did you find anything useful in this Excel tutorial? Do you know a better way to achieve no trailing spaces in Excel? Comment below!

About the Author

Bipasha Nath

Bipasha Nath

Bipasha is a technical content writer with 10+ years of experience in the technology industry. She previously worked in a SaaS software development company focused on SharePoint, OneDrive, Word, Excel, Word solutions.

Related Posts

Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get the Latest Microsoft Excel Tips

Follow Us

Follow us to stay up to date with the latest in Microsoft Excel!

Subscribe for awesome Microsoft Excel videos 😃