Trim function is not removing multiple spaces within a string

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
I am pulling my hair out over this one.

I have a name that should be LastName1(space)LastName2(comma)(space)FirstName

Example: De Lahoya, Oscar

For some reason the Trim function 'apparently' is leaving two spaces between the two last names.

VBA Code:
Sub PeskyName()
'
    Dim PeskyName   As String
'
    PeskyName = " De  Lahoya, Oscar "
'
    Debug.Print PeskyName
    Debug.Print Trim(PeskyName)
End Sub

I have also tried an ASCII check:

VBA Code:
                    Case 65 To 90, 97 To 122, 32, 44 To 45: 'Keep only Upper/Lowercase, space, comma, hyphen ... skip/strip anything else found in the name

Still shows '2 spaces' between the last names.

I just don't know what else to try.

Any ideas?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
VBA Code:
Sub PeskyName()
'
    Dim PeskyName   As String
'
    PeskyName = " De  Lahoya, Oscar "
'
    Debug.Print PeskyName
    
    Debug.Print Replace(Trim(PeskyName), "  ", " ")
End Sub

TRIM only works for space in the beginning or the end of the string. So after TRIM, Replace two space into one will work.
 
Upvote 0
I usually just use the worksheet function.
VBA Code:
Debug.Print Application.Trim(PeskyName)
 
Upvote 0
Solution
VBA Code:
Sub PeskyName()
'
    Dim PeskyName   As String
'
    PeskyName = " De  Lahoya, Oscar "
'
    Debug.Print PeskyName
   
    Debug.Print Replace(Trim(PeskyName), "  ", " ")
End Sub

TRIM only works for space in the beginning or the end of the string. So after TRIM, Replace two space into one will work.
Crap! I falied to read the fine print.

VBA Code:
There is one important thing to know when using the TRIM function in VBA –  it will only remove the leading and trailing spaces. It will not remove the extra spaces in between words (which the in-built TRIM function in the worksheet does).

Double crap! Thank you @Hileolane for steering me into a new direction.
 
Upvote 0
I usually just use the worksheet function.
VBA Code:
Debug.Print Application.Trim(PeskyName)
Thank you @Alex Blakenburg & @Hileolane!

Dang, I wasted time trying to code around the Trim function, I failed to see the difference between the vba and worksheet functionality of the Trim function. :rolleyes:

I now see why my workaround also failed.
 
Upvote 0
And for some reason that I don't understand, if you use Application.Trim you can use IsError while if you use WorksheetFunction.Trim or Application.Worksheet.Trim you have to use VBA error handling ie On Error and Err =.

This is true for using all WorksheetFunctions in VBA.
See Chip (Charles) Pearson's article here:
Calling Worksheet Functions In VBA
 
Upvote 0
Leave it to microsoft to not dot all of the 'i's.

That is why they get paid the big $$$. Job security with needed fixes. LOL
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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