Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-06
4
Medium Priority
?
469 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:sir plus
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 2000 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"
1
 
LVL 49

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:sir plus
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

916 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