Remove spaces within text string VBA

johnrlalor

New Member
Joined
Feb 14, 2014
Messages
40
I have a range of data (Cells G4: G52) which can be populated incorrectly with spaces, I am trying to implement some VBA code which will automatically review this range and remove spaces where appropriate.
Any thoughts?
Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

With VBA, the instruction Replace will do the job ...

But, beforehand, you need to determine the nature of your spaces ... char(32), char(160), etc ...

Hope this will help
 
Upvote 0
To remove leading spaces, trailing spaces and intermediate double(or more) spaces :

[G4:G54] = [Trim(G4:G52)]
 
Upvote 0
The TRIM function in Excel removes all leading, trailing and multiple spaces. TRIM in VBA only removes leading and training spaces.

An easy way to overcome this in VBA is by using Application.Trim instead of just Trim. See below, taken from the VBA Immediate Window.

strInput = " President Barack Obama "
?Trim(strInput)
President Barack Obama
?Application.Trim(strInput)
President Barack Obama
 
Upvote 0
The TRIM function in Excel removes all leading, trailing and multiple spaces. TRIM in VBA only removes leading and training spaces.

An easy way to overcome this in VBA is by using Application.Trim instead of just Trim. See below, taken from the VBA Immediate Window.

strInput = " President Barack Obama "
?Trim(strInput)
President Barack Obama
?Application.Trim(strInput)
President Barack Obama

Edit to say the original string had loads of spaces within it, but the forum has trimmed these out. I hope this tip might prove useful for some.
 
Upvote 0
Hi

Remark:


Edit to say the original string had loads of spaces within it, but the forum has trimmed these out.

A simple way to avoid this is to use a code box

Code:
"    President     Barack     Obama       "
?Trim(strInput)
President     Barack     Obama
?Application.Trim(strInput)
President Barack Obama
 
Last edited:
Upvote 0
The TRIM function in Excel removes all leading, trailing and multiple spaces. TRIM in VBA only removes leading and training spaces.

An easy way to overcome this in VBA is by using Application.Trim instead of just Trim. See below, taken from the VBA Immediate Window.

Not true.
Have you tried the code I posted? It removes leading, trailing and multiple intermediate spaces : [G4:G54] = [Trim(G4:G52)]

The code is merely a shorter version of : [G4:G54] = Evaluate("Trim(G4:G52)")

It could, of course be written as : [G4:G54] = Application.Trim([G4:G54])
 
Upvote 0
By way of further explanation, the code I posted uses the worksheet TRIM function (the same as using Application.Trim)
 
Upvote 0
The TRIM function in Excel removes all leading, trailing and multiple spaces. TRIM in VBA only removes leading and training spaces.

That is how I understand it as well.

Code:
Sub TrimTest()
    Dim S As String

    S = "    President     Barack     Obama       "
    Debug.Print "'" & S & "' -->Length = " & Len(S)
    Debug.Print "'" & VBA.Trim(S) & "' -->Length = " & Len(VBA.Trim(S)) & " (VBA Trim)"
    Debug.Print "'" & Application.Trim(S) & "' -->Length = " & Len(Application.Trim(S)) & " (Application/Worksheet Trim)"
End Sub

Code:
'    President     Barack     Obama       ' -->Length = 41
'President     Barack     Obama' -->Length = 30 (VBA Trim)
'President Barack Obama' -->Length = 22 (Application/Worksheet Trim)
 
Upvote 0
quote_icon.png
Originally Posted by Eli PanicThe TRIM function in Excel removes all leading, trailing and multiple spaces. TRIM in VBA only removes leading and training spaces.



That is how I understand it as well.

Quite right.
Please note that the code I posted does not use TRIM in VBA.
It uses the worksheet TRIM function.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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