Remove unnecessary spaces in cells in column "J"

harzer

Board Regular
Joined
Dec 15, 2021
Messages
159
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
How can we remove (Quickly) all unnecessary spaces: at the beginning, at the end and inside the cells that are in the cells of column "J".
We start at line 2 in "J2" and end at the last cell of column "J".
When I talk about inside the cells in column "J", I mean remove the duplicate spaces so that there is only one space between each word in the cells in column "J".
I remain at your disposal for other additional information if necessary.
Good programming.
Thank you for your contributions.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This VBA should do what you want:
VBA Code:
Sub RemoveExtraSpaces()

    Dim lr As Long, r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column J with data
    lr = Cells(Rows.Count, "J").End(xlUp).Row
    
'   Loop through all cells in column J starting with line 2
    If lr > 1 Then
        For r = 2 To lr
'           See if there is an entry in column J
            If Cells(r, "J") <> "" Then
'               Trim extra spaces
                Cells(r, "J").Value = Application.Trim(Cells(r, "J").Value)
            End If
        Next r
    End If

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
This code was written by jindon.
VBA Code:
Sub RemoveSpaces()
    With ActiveSheet.Columns("J:J")
        .Value = Evaluate("if(row(" & .Address & "),clean(trim(" & .Address & ")))")
    End With
End Sub
 
Upvote 0
This code was written by jindon.
VBA Code:
Sub RemoveSpaces()
    With ActiveSheet.Columns("J:J")
        .Value = Evaluate("if(row(" & .Address & "),clean(trim(" & .Address & ")))")
    End With
End Sub
Only caveat there is that includes row 1, and they wanted to start at row 2.
Depending on what is in cell J1, it may or may not matter.
 
Upvote 0
How about:
VBA Code:
Sub RemoveSpaces()
    With ActiveSheet.Range("J2", Range("J" & Rows.Count).End(xlUp))
        .Value = Evaluate("if(row(" & .Address & "),clean(trim(" & .Address & ")))")
    End With
End Sub
 
Upvote 0
Solution
Hello mumps & Joe4,
Thank you for your respective codes.
Both codes give me the results I want and they are fast.
Thank you both for your quick responses.
Greetings.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top