Solved

Trim(" my string      ") produces " my string      "

Posted on 2013-02-06
4
449 Views
Last Modified: 2013-02-06
I ran into this using vba code but was able to reproduce it in the immediate window

Enter
?">" & trim("SALLY ALLAN                  ") & "<"
in the immediate window then hit enter

Result:
>SALLY ALLAN                  <
I am after >SALLY ALLAN<

Have tried  
?">" & Application.trim("SALLY ALLAN                  ") & "<"
&
?">" & Strings.trim("SALLY ALLAN                  ") & "<"
As well as
?">" & Application.trim(cstr("SALLY ALLAN                  ")) & "<"
&
?">" & Strings.trim(cstr("SALLY ALLAN                  ")) & "<"


All give >SALLY ALLAN                  <
I need >SALLY ALLAN<

Sure I can parse it only this stuff is supposed to be native so what's going on??
Thanks in advance -easy points for someone with a clear mind..........

PS
No librarys are missing and
on error goto 0
is in effect

Also tried in code
   Dim strSearch as string
    strSearch = Replace("SALLY ALLAN                  ", vbCr, " ")
    strSearch = Replace(strSearch, vbLf, " ")
    strSearch = Trim(strSearch)
    debug.print ">" & strSearch & "<"
Still >SALLY ALLAN                  <

Resetting all librarys did not work either
(running C:\Program Files (x86)\Microsoft Office\Office14\Excel.exe   /regserver).
:-/
0
Comment
Question by:sirplus
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38862711
the string was not ending with sapce character. i just copied the string you posted and checked it's showing unicode 3F character from Latin block. from where did you get the input? if you try typing following statement by your self not copy paste,
?trim("SALLY ALLAN     ") returns you "SALLY ALLAN"
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 38862713
When I do

?">" & trim("SALLY ALLAN                  ") & "<"

I get

>SALLY ALLAN<

Try

?">" & VBA.trim("SALLY ALLAN                  ") & "<"

If that works for you as I suspect it will, go to Tools|References and you'll probably find that one is "Missing".
0
 
LVL 40
ID: 38862721
Are you sure that what looks like spaces are spaces? It could be another ASCII character that does not display. If the thing was copied from Word, then it

Try

? Asc(Mid("SALLY ALLAN                  ",12,1))

The result should be 32, the ASCII code for a space. If not, then you have special character that you will need to replace by " " before trimming the string.
0
 
LVL 5

Author Comment

by:sirplus
ID: 38862776
I jigged up this function to clean it up
Function fStrLettersAndBlanksOnly(ByVal pstrString As String)
    Dim i As Integer
    For i = Len(pstrString) To 1 Step -1
        Select Case Asc(Mid(pstrString, i, 1))
            Case 32 To 126
                        
            Case Else
                pstrString = Left(pstrString, i - 1) & Right(pstrString, Len(pstrString) - i)
            End Select
    Next i
    fStrLettersAndBlanksOnly = pstrString
End Function

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question