Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Nesting "RIGHT", "MID", and "FIND" in a formula

Latest post Tue, Jun 17 2008 9:53 AM by Garyb444. 18 replies.
  • Thu, May 29 2008 3:48 PM

    • Gary2m
    • Top 150 Contributor
    • Joined on Thu, May 29 2008
    • Posts 2
    • Points 58

    Nesting "RIGHT", "MID", and "FIND" in a formula

     I am trying to get the correct formula to extract the last word in a string.

    Data                                                              Correct Answer

    John Doe                                                          Doe

    John A. Downer                                                Downer

    John A. and Jane Smithhaven                          Smithhaven

     

    If anyone can give me the correct fomula for extracting this data I would greatly appreciate it.

    Filed under:
    • Post Points: 37
  • Thu, May 29 2008 4:52 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 297
    • Points 4,959
    • MVP

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Gary

    I always use a UDF (User Defined Function) like that below.

    Public Function FindLastSpace(sInput As String) As String
    Dim iLastSpacePos As Integer
    Dim iLengthOfString As Integer
    
    Application.Volatile True
    
    iLastSpacePos = InStrRev(sInput, " ", -1)
    iLengthOfString = Len(sInput)
    FindLastSpace = Mid(sInput, iLastSpacePos + 1, iLengthOfString - (iLastSpacePos - 1))
    End Function

     

    If you paste the code into a standard module in the workbook (Alt+F11, select 'insert module' and paste code there. close the IDE), now you can use it like a standard worksheet formula, e.g if you name is in A1, then in B1 enter
    =FindLastSpace(A1)
    and copy down as far as you need.

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under: , ,
    • Post Points: 21
  • Fri, May 30 2008 5:16 AM In reply to

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Sans VBA

     

    =MID(A1,FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1,99)

    Regards

    Bob

    • Post Points: 53
  • Fri, May 30 2008 5:43 AM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 297
    • Points 4,959
    • MVP

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Bob

    That was why I went avec VBA. Wish I could take the time to sit and work out some of these great function combinations!

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 5
  • Fri, May 30 2008 9:00 AM In reply to

    • Gary2m
    • Top 150 Contributor
    • Joined on Thu, May 29 2008
    • Posts 2
    • Points 58

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

     Bob, since I'm not a Visual Basics guy, thanks for the formula.  I've been working on this for over two days and I never would have figured it out.

    Thanks again.........

    • Post Points: 21
  • Fri, May 30 2008 11:12 AM In reply to

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    I'll give you a quick synopsis Gary to help you develop your own skills.

    The key problem is finding out where the last space is, or how many spaces there are, and this is done by looking at the length of the cell value if it didn't have any spaces in it ( the LEN(SUBSTITUTE(A1," ","")) ...) and taking that figure away from the length of the whole cell ( LEN(A1)-LEN(SUBSTITUTE(A1," ","")) ...). So for John Doe we get 1, John A.Downer 2, John A. and Jane Smithhaven 4).

    We can then use SUBSTITUTE again to replace a paricular instance of a string with another, in other words, replace the final space with a non-used character, I use CHAR(1). So SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))) changes just the last space.

    We then use FIND to determine the location of that unused character, FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ","")))) so we know where to extract the data from.

    And finally, we use MID to get that last space +1 as our start point, and I use 99 as the length because MID does not choke if the length is too long, so we get away with a largeish arbitrary number, MID(A1,FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1,99)

    Hope that helps.

    Regards

    Bob

    • Post Points: 21
  • Fri, May 30 2008 11:18 AM In reply to

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    BTW, let that be a lesson to you Smile. NEVER work on something in Excel for 2 days without getting anywhere; ask for help, a pointer, or a suggestion, time is too precious.

    Regards

    Bob

    • Post Points: 5
  • Fri, May 30 2008 11:33 AM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 297
    • Points 4,959
    • MVP

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Bob

    Thanks for setting that precedent, can we expect clear concise explanations of your solutions each time in future Big Smile

    Great job!

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 21
  • Fri, May 30 2008 1:11 PM In reply to

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Nick,

     

    You know that normally my formulae are totally obvious, so they self-document, and need no further explanation.Storm

    Regards

    Bob

    • Post Points: 21
  • Fri, May 30 2008 2:01 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 297
    • Points 4,959
    • MVP

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Bob

     

    I have an instant use for the formulae in this thread, but had to add TRIM around each cell refernce as the data comes from an iSeries and has fixed width padding. That done it worked great.

    Could have used ny UDF or course, but I'm sure yours would be faster on 25,000+ records

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 21
  • Fri, May 30 2008 2:06 PM In reply to

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Serendipity.

    Regards

    Bob

    • Post Points: 5
  • Sun, Jun 8 2008 4:13 AM In reply to

    • hrlngrv
    • Top 10 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 67
    • Points 1,055

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    Also sans VBA, but with the following ancillary overhead: a very hidden worksheet named HiddenWorksheet and defined names NCHARS referring to 255 (or some other biggish integer < 32768) and seq referring to

    =ROW(INDEX(HiddenWorksheet!$1:$65536,1,1):INDEX(HiddenWorksheet!$1:$65536,NCHARS,1))

    use the formula

    =MID(A1,LOOKUP(2,1/(MID(A1,seq,1)=" "),seq)+1,NCHARS)

    But a better solution, if allowed, would be to use Longre's MOREFUNC.XLL add-in and formulas like

    =WMID(A1,WORDCOUNT(A1))

     

    • Post Points: 5
  • Mon, Jun 9 2008 11:43 AM In reply to

    • SeanLeal
    • Top 200 Contributor
    • Joined on Mon, Jun 9 2008
    • Posts 2
    • Points 58

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

     This improves on previous posts (in my opinion) because it does not have an arbitrary limit of 99 (or any) characters:

    =RIGHT(A1,LEN(A1)-FIND("*",SUBSTITUTE(A1," ","*",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))

    What it's doing is finding the total number of spaces in the cell by subtracting the

    length without spaces from the total length. Knowing that number, it uses it to replace the last space with an asterisk. From that point, it's easy; just take everything to the right of the asterisk.  NOTE: If you have asterisks in your text, you can change the function to use tildes (~) or back apostrophies (`)

    Courtesy of:

    http://www.exceltip.com/st/Extract_the_last_word_in_a_cell_in_Microsoft_Excel/368.html

     

    • Post Points: 21
  • Wed, Jun 11 2008 2:34 PM In reply to

    • hrlngrv
    • Top 10 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 67
    • Points 1,055

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    SeanLeal:
    This improves on previous posts (in my opinion) because it does not have an arbitrary limit of 99 (or any) characters:

    =RIGHT(A1,LEN(A1)-FIND("*",SUBSTITUTE(A1," ","*",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))

    Oh, so colorful!

    Let's consider whether this is, in fact, an improvement. Like all SUBSTITUTE approaches, it requires the use of an indicator character to give the location of the particular character sought. FAR BETTER to use a control/non-graphic character, such as CHAR(1) which Bob Phillips used, because such characters are far less likely to appear in arbitrary strings than any graphic characters.

    Next, MID with 3rd argument 32767 will always be more efficient then RIGHT formulas because they need fewer function calls. And since strings can never exceed 32767 characters, there's no arbitrary limit either. It's a pity Excel's MID's 3rd argument isn't optional like VBA's Mid function.

     

    • Post Points: 21
  • Thu, Jun 12 2008 1:17 AM In reply to

    • SeanLeal
    • Top 200 Contributor
    • Joined on Mon, Jun 9 2008
    • Posts 2
    • Points 58

    Re: Nesting "RIGHT", "MID", and "FIND" in a formula

    As you can see from my creative color scheme, clarity is paramount when I consider the 'best' option.  As someone who supports many levels of Excel users I need to be able to clearly explain what a formula does, preferably in English.  It also has to be clear enough for basic level users to modify it for their uses.  (The colors help.)

    I don't doubt your solution would run faster, and I agree char(1) is a much safer replacement character, but it's much easier to explain * or ~ to an administrative assistant who just needs to pull out the last name of their manager's contact list.

    ... Which is why it is an improvement for me.  Smile

     

    • Post Points: 37
Page 1 of 2 (19 items) 1 2 Next > | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.