Remove Trailing Spaces from Cells in Excel

This post will guide you how to remove leading and trailing spaces in cells in Excel. How do I remove leading or trailing spaces in a range of cells with a formula in Excel. How to remove trailing spaces from cells with VBA Macro in Excel.

Remove Trailing Spaces from Cells with Formula

If you want to remote all leading and trailing spaces from test string in one or more cells, you can use a formula based on the TRIM function.

For example, you have a list of data in range B1:B4 contain text string values, and you want to strip all leading and trailing spaces in the given range of cells, how to achieve it. You can use the following formula.


Type this formula into a blank cell and then press Enter key, and drag the AutoFill Handle over other cells to apply this formula.

remove trailing spaces1

This formula will remove both leading and trailing spaces from text string in cells.

Remove Trailing Spaces from Cells with VBA Macro

You can also use an Excel VBA Macro to remote all trailing spaces from a given range of cells. Just do the following steps:

#1 select one range of cells that you want to remove trailing spaces

remove trailing spaces4

#2 open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.

Get the position of the nth using excel vba1

#3 then the “Visual Basic Editor” window will appear.

#4 click “Insert” ->”Module” to create a new module.

convert column number to letter3

#5 paste the below VBA code into the code window. Then clicking “Save” button.

remove trailing spaces2

Sub RemovTrailSpaces()
    Dim cel  As Range
    For Each cel In Selection.Cells
        cel = Trim(cel)
End Sub

#6 back to the current worksheet, then run the above excel macro. Click Run button.

remove trailing spaces3

#7 Let’s see the result.

remove trailing spaces5

Related Functions

  • Excel TRIM function
    The Excel TRIM function removes all spaces from text string except for single spaces between words.  You can use the TRIM function to remove extra spaces between words in a string.The syntax of the TRIM function is as below:= TRIM (text)….



Related Posts

Find and Replace Multiple Values

This post will guide you how to find and replace multiple values at once with VBA macro or using formula  in Excel. How do I make multiple find and replace in Excel. Suppose that you have a few cells containing ...

VBA Macro For VLOOKUP From Another Sheet

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

How To Insert Comments in Protected Worksheet in Excel

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...