How to remove all spaces between numbers or words

This post will describe two quick ways to remove all space characters between numbers or words in excel. How to remove unwanted spaces from cells in a select range of cells in Excel.

Assuming that you have a list of data with text string and numbers in one cell, and you want to remove all spaces between text character and numbers in those cells. You can use Excel Find & replace or SUBSTITUTE function. This post will guide you how to use those two ways to remove all spaces.

1. Remove all spaces using Find and Replace option

The below steps will teach you how to remove all spaces between characters and numbers using the standard Excel Find & Replace option:

Step 1# select the range of cells that you want to remove spaces

remove spaces 1

Step 2# go to “Home” Tab, then click “Find & Select” -> “Replace…”, then the Find and Replace dialog box will appear.  Or press “CTRL+H” shortcut to open the “Find and Replace” dialog box.

remove spaces 1

Step 3# press the space bar in the Find what text box, and do not input any character in the “replace with” box. Then click Replace All button.  Click “OK’ button.

remove spaces3

You will see that how many spaces have been removed from the above screenshot.

Step 4# let’s see the last result:

remove spaces4

2. Remove all spaces using SUBSTITUTE function

You may want to remove all space characters between numbers or characters using an excel formula, then you can use the SUBSTITUTE function to create a formula as follows:

=SUBSTITUTE(A1," ", "")

You just need to enter the above formula in another cell, such as: B1, then press Enter.

remove spaces5

Then you can select cell B1, then drag the AutoFill Handle down the other cell that you want to apply this formula.

remove spaces6

3. remove all spaces between numbers or words using VBA Code

For the third method, we’ll delve into the power of VBA (Visual Basic for Applications) code to programmatically remove spaces between numbers or words in Excel. This method offers advanced customization, allowing you to tailor the solution to fit your specific data cleaning requirements.”

Press ‘Alt + F11‘ to open the Visual Basic for Applications editor.

Right-click on the project in the editor, select ‘Insert,’ and choose ‘Module‘ to add a new module.

Copy and paste the following VBA code into the module:

Sub RemoveSpacesWithPrompt()
    Dim sourceRange As Range
    Dim destinationCell As Range
    Dim resultCell As Range
    
    ' Prompt user to select source range
    On Error Resume Next
    Set sourceRange = Application.InputBox("Select the source range with spaces", Type:=8)
    On Error GoTo 0

    ' Exit if the user cancels the selection
    If sourceRange Is Nothing Then
        Exit Sub
    End If

    ' Prompt user to select starting destination cell
    On Error Resume Next
    Set destinationCell = Application.InputBox("Select the starting destination cell", Type:=8)
    On Error GoTo 0

    ' Exit if the user cancels the selection
    If destinationCell Is Nothing Then
        Exit Sub
    End If

    ' Loop through each cell in the destination range and remove spaces
    For Each resultCell In destinationCell.Resize(sourceRange.Rows.Count, sourceRange.Columns.Count)
        resultCell.Value = Replace(sourceRange.Cells(resultCell.Row - destinationCell.Row + 1, resultCell.Column - destinationCell.Column + 1).Value, " ", "")
    Next resultCell
End Sub

Close the editor and press ‘Alt + F8’

Choose ‘RemoveSpacesWithPrompt‘ and click ‘Run.’

A dialog box will appear, prompting you to select the source range with spaces to remove. Click and drag to select the desired range, then click Ok button.

Another dialog box will appear, prompting you to select the destination cell to place the result. Click on the desired destination cell, then click Ok button.

The VBA code will remove spaces from the selected source range and place the last result in the chosen destination cell.

4. Video: remove all spaces between numbers or words

This Excel video tutorial where we’ll explore three effective methods to remove all spaces between numbers or words. We dive into the first method using the SUBSTITUTE function, the second method employing the ‘Find and Replace’ feature, and the third method utilizing VBA code.

https://youtu.be/Xy-Ecxj1UAA

5. Related Formulas

6. Related Functions

  • Excel Substitute function
    The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string.The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE  (text, old_text, new_text,[instance_num])….

Leave a Reply