VBA: Remove Characters from Left or Right Side of Variable Length String

February 18th, 2005 | Categories: Strings | Tags: , , ,

If you always know the length of a string, it’s easy to remove characters from it. Example: If you have a string that is 10 characters and you want to remove 1 character from the Left side, simply return the right 9 characters:

msgbox Right(Mystring, 9)

This doesn’t work for a variable length string, or one which you don’t know beforehand it’s length. In this case you can use the formula (Length – N) to designate how many characters to extract:

MsgBox Right(Mystring, Len(Mystring) – 1)

Where 1 is the number of characters to remove from the left side of the string. This will return the string minus the left most character.

To remove characters from the right side of a string, replace Right with Left

  1. February 18th, 2005 at 15:16
    Reply | Quote | #1

    Hi Mark!

    I tend to use the Mid function.

    eg.
    MsgBox Mid(Mystring, 2)

    Cheers,
    Rob

  2. Mark
    February 18th, 2005 at 19:43
    Reply | Quote | #2

    Hi Rob,

    Never thought of mid like that, just extracting from the middle. The Mid doesn’t work when removing characters from the right side of a string, unless I missed it.

  3. Andy Pope
    February 19th, 2005 at 03:02
    Reply | Quote | #3

    Hi Mark,

    The MID function has an optional argument to specify the length

    Msgbox Mid(MyString,1, Len(Mystring) – 1)

  4. Mark
    February 19th, 2005 at 03:38
    Reply | Quote | #4

    Thanks andy. I see it now.

    Must be a Left(brain) Right(brain) thing, which function people naturally gravitate towards, or something like that.

  5. February 19th, 2005 at 13:54
    Reply | Quote | #5

    I tend to use the Mid fuction, it’s the only real option when you’r not sure of the lenght of the stringd involved.

    Speaking of left a right sides – which way does the sun shine on this blog? both the left and right edges have shadows – not that it matters any of course, but i just noticed it!

  6. Mark
    February 19th, 2005 at 15:34
    Reply | Quote | #6

    LOL, the suns reflecting off my forehead straight onto the screen. Actually I noticed the odd shadows when I put this layout up and thought “nobody will notice”.

    The tan and blues are from taking a color picker to a screenshot of Excel.

    I’m thinking of tweaking the layout again for the fun of it. -edit-

    This site is my first leap onto the web, so everything from playing with advt’s, to layouts, to sydicating this site on others (exceltip (not anymore)), to some content: is an experiment just as much as for function.

  7. February 20th, 2005 at 08:25
    Reply | Quote | #7

    Mark, it’s my opinion that this is the prettiest excel blog on the web!!! – i like the layout as it is – hey but if you wanta change it don’t let me stop you!

  8. Mark
    February 20th, 2005 at 22:10
    Reply | Quote | #8

    MIE is coming along nicely also, thanks for the big link. Let me know when you want a reciprocal.

    I know your not much on doing a blog yourself, however an RSS feed when you add articles would be awesome. From an RSS reader’s standpoint anyway.

  9. February 21st, 2005 at 11:39
    Reply | Quote | #9

    thanks – I’m (to coin an English phase) “blowing out of my as#) at the mo, but I have just got http://www.methodsinexcel.co.uk booked, and will be moving the

  10. Mark
    February 21st, 2005 at 21:21

    Excel Left Function VS. Excel Mid Function

    Excel Left Function wins the GoogleFight

    lol

  11. Glenn
    February 1st, 2009 at 02:28

    Help if you can
    I need to remove the hyphen from the middle of a text string
    example
    I have 4011-5555
    I need 40115555
    is there a way to remove the character from a range of cells withou manually removing it

  12. Steve
    March 19th, 2009 at 11:57

    I’m also looking for this function if anyone knows it. I’m having to make do with the following at the moment,

    oldstring = “111-111″

    newstring = Left(oldstring, InStr(oldstring, “-”) – 1) & Right(oldstring, InStr(oldstring, “-”) + 1)

    Its a bit naff and only works if theres 1 instance of “-”.

    thanks.

  13. Steve
    March 19th, 2009 at 11:58

    sorry, should read.

    newstring = Left(oldstring, InStr(oldstring, “-”) – 1) & Right(oldstring, InStr(oldstring, “-”) – 1)

  14. Gordon
    March 20th, 2009 at 10:37

    @Steve – If I understand what your after, this is easy with Replace:

    newString = Replace(oldString, “-”, “”)

  15. Jason
    July 23rd, 2009 at 18:44

    I am looing at the post for removing text from a string MsgBox Right(Mystring, Len(Mystring) – 1). This places a msgbox on screen but does not change the text in the cell. How do I change the text string in the cell?