Deleting excessive spaces

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I have up till now been using the code below to delete excessive spaces

Dim r As Range
Dim Target As Range

Set Target = ActiveSheet.Range("C1:C3000")

For Each r In Target
If Len(r.Value) <> Len(Trim(r.Value)) Then
r.Value = Application.Trim(r.Value)
End If
Next


Which seems to have worked well up till now

I have discovered that if I have a phrase in column C for example

testing testing

the code does not pick up on the fact that this has two spaces between both words, I had thought it would delete all excessive spaces, not just what is before and after

Does anyone have any ideas please ?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Code:
Sub Excess()

    Dim r As Range, Target As Range
    
    Application.ScreenUpdating = False
    
    Set Target = ActiveSheet.Range("C1:C3000")
    
    For Each r In Target
        r = WorksheetFunction.Trim(r)
    Next

End Sub
 
Upvote 0
Not sure why!!!
This worked for me.
Code:
[COLOR="Navy"]Sub[/COLOR] MG27May15
[COLOR="Navy"]Dim[/COLOR] r [COLOR="Navy"]As[/COLOR] Range, Rng [COLOR="Navy"]As[/COLOR] Range
Application.ScreenUpdating = False
    [COLOR="Navy"]Set[/COLOR] Rng = ActiveSheet.Range("C1:C3000")
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] r [COLOR="Navy"]In[/COLOR] Rng
        r.value = Replace(r.value, " ", "")
        [COLOR="Navy"]Next[/COLOR]
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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