Trim in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Trim (VBA function) is not working here. The output message I go is " hello ................ there"
Anything wrong with the code below? Thank you so much

Code:
Sub mytrim()
    Dim s As String
    s = Trim("               hello                             there   ")
    MsgBox s
End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The VBA trim function only removes leading/trailing spaces.
 
Upvote 0
Code:
Sub MyTrim()
Dim s$, i&
Dim Arr As Variant
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")


s = Trim("               hello                             there   ")
Arr = Split(s, " ")
For i = LBound(Arr) To UBound(Arr)
    If Not Arr(i) = "" Then dict(Arr(i)) = dict(Arr(i)) + 1
Next i
ReDim Arr(1 To dict.Count)
For i = 1 To dict.Count
    Arr(i) = dict.Keys()(i - 1)
Next i
s = Join(Arr, " ")
MsgBox s
End Sub
 
Upvote 0
Or simply
Code:
s = Application.Trim("               hello                             there   ")
 
Upvote 0
If you want your string to result in "hello there", use Fluff's method.

If you want the string to be "hello there" then post #4 would be a better fit.
 
Upvote 0
:confused:
They look tome to be exactly the same ;)

Also if the string was
Code:
"    hello           hello                             there   "
your code would result in "hello there" rather than "hello hello there"
 
Upvote 0
To be fair they look the same to me too. Is there meant to be a difference?
 
Upvote 0
To be fair they look the same to me too. Is there meant to be a difference?

I didn't notice until it was too late to correct. I thought the OP was unclear as to the desired output. Are we looking to remove all of the additional spaces? Are we just looking to remove the leading and trailing spaces?

My attempt was to remove all of the spaces to leave all of the words separated by a single space. By using a collection instead of a dictionary approach, this is possible.

Code:
Sub MyTrim()

Dim s$, i&
Dim Arr As Variant
Dim Coll As New Collection


s = Trim("     hello          hello                             there   ")
Arr = Split(s, " ")
For i = LBound(Arr) To UBound(Arr)
    If Not Arr(i) = "" Then Coll.Add Arr(i)
Next i
ReDim Arr(1 To Coll.Count)
For i = 1 To Coll.Count
    Arr(i) = Coll(i)
Next i
s = Join(Arr, " ")
MsgBox s
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,628
Members
452,661
Latest member
Nonhle

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