Consulting

Results 1 to 8 of 8

Thread: Need trim to include leading space

  1. #1
    VBAX Mentor Sir Babydum GBE's Avatar
    Joined
    Mar 2005
    Location
    Cardiff, UK
    Posts
    499
    Location

    Need trim to include leading space

    The title does what it says on the tin...

    How do I get TRIM() formula to also take out leading space? Sometimes there's more than one leading space - but trim seems to take care of all bar the leading one.

    Any ideas please?

    BD
    Have a profound problem? Need a ridiculous solution? Post a question in Babydum's forum

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Are you sure about that BD?

    [vba]

    Dim sTest As String

    sTest = " hello"
    Debug.Print Len(sTest) & " - " & Len(Trim(sTest))
    sTest = " hello"
    Debug.Print Len(sTest) & " - " & Len(Trim(sTest))
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    BD, what is 'tin' and what is 'bar'? Pease reaf dwice befose porting...
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  4. #4
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    If your TRIM formula is leaving a space, it's probably not a "normal" space, but a CHAR(160), especially if the data came from the web.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    VBAX Mentor Sir Babydum GBE's Avatar
    Joined
    Mar 2005
    Location
    Cardiff, UK
    Posts
    499
    Location
    tstav - "Does what it says on the tin" here means that the post's subject is the same as the question. "Bar" means "except". apologies.

    XLD - I'm not sure what your reply means. But when you ask "am i sure about that"... If I type a few leading spaces into a text input cell - then trim removes all the spaces. But it doesn't seem to work on the data that's been imported. That's where I need it to work.

    I've just seen Rory's reply (cheers Rory) - so given that these spaces may be CHAR(160) thingimmyjigs (sorry tstav) can they be gotten rid of by a formula without taking out genuine spaces between words?
    Have a profound problem? Need a ridiculous solution? Post a question in Babydum's forum

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It means I am showing you that TRIM works on spaces.

    Try this

    [vba]

    Dim sTest As String

    sTest = Chr(160) & "hello"
    sTest = " " & sTest
    Debug.Print Len(sTest) & " - " & Len(Trim(Replace(sTest, Chr(160), "")))
    [/vba]

    or get Dave McRitchie's all-encompassing TRIMALL function http://www.mvps.org/dmcritchie/excel/join.htm#trimall
    Last edited by Bob Phillips; 04-24-2008 at 07:46 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Are you doing this in a worksheet formula? If so, the equivalent to xld's VBA is:
    =TRIM(SUBSTITUTE(A1,CHAR(160),""))
    Regards,
    Rory

    Microsoft MVP - Excel

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    PS You can check the character using:
    =CODE(TRIM(A1))
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •