Extract the last substring from a cell

后端 未结 9 1839
逝去的感伤
逝去的感伤 2020-12-01 07:57

I have names in a column. I need to split just the last names from that column into another column.

The last name is delimited by a space from the right side.

相关标签:
9条回答
  • 2020-12-01 08:10

    RIGHT return whatever number of characters in the second parameter from the right of the first parameter. So, you want the total length of your column A - subtract the index. which is therefore:

    =RIGHT(A2, LEN(A2)-FIND(" ", A2, 1))
    

    And you should consider using TRIM(A2) everywhere it appears...

    0 讨论(0)
  • 2020-12-01 08:13

    Try this function in Excel:

    Public Shared Function SPLITTEXT(Text As String, SplitAt As String, ReturnZeroBasedIndex As Integer) As String
            Dim s() As String = Split(Text, SplitAt)
            If ReturnZeroBasedIndex <= s.Count - 1 Then
                Return s(ReturnZeroBasedIndex)
            Else
                Return ""
            End If
        End Function
    

    You use it like this:

    First Name (A1) | Last Name (A2)

    Value in cell A1 = Michael Zomparelli

    I want the last name in column A2.

    =SPLITTEXT(A1, " ", 1)
    

    The last param is the zero-based index you want to return. So if you split on the space char then index 0 = Michael and index 1 = Zomparelli

    The above function is a .Net function, but can easily be converted to VBA.

    0 讨论(0)
  • 2020-12-01 08:13

    Try this:

    Right(RC[-1],Len(RC[-1])-InStrRev(RC[-1]," "))
    
    0 讨论(0)
  • 2020-12-01 08:15

    This works, even when there are middle names:

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

    If you want everything BUT the last name, check out this answer.

    If there are trailing spaces in your names, then you may want to remove them by replacing all instances of A2 by TRIM(A2) in the above formula.

    Note that it is only by pure chance that your first formula =RIGHT(A2,FIND(" ",A2,1)-1) kind of works for Alistair Stevens. This is because "Alistair" and " Stevens" happen to contain the same number of characters (if you count the leading space in " Stevens").

    0 讨论(0)
  • 2020-12-01 08:19

    If you want to get the second to last word in a text, you can use this macro as a function in your spreadsheet:

    Public Function Get2ndText(S As String) As String
    
    Dim sArr() As String
    Dim i As Integer
    sArr = Split(S, " ")
    
    'get the next to the last string
    i = UBound(sArr) - 1
    Get2ndText = sArr(i)
    
    End Function
    

    Then in your spreadsheet B1 as the text:

    CURRENT OWNER 915 BROADWAY ST HOUSTON TX 77012-2126
    

    in B2 your formula would be:

    =Get2ndText(B1)
    

    The result would be

    TX
    
    0 讨论(0)
  • 2020-12-01 08:21

    Try this:

    =RIGHT(TRIM(A2),LEN(TRIM(A2))-FIND(" ",TRIM(A2)))
    

    I was able to copy/paste the formula and it worked fine.

    Here is a list of Excel text functions (which worked in May 2011, and but is subject to being broken the next time Microsoft changes their website). :-(

    You can use a multiple-stage-nested IF() functions to handle middle names or initials, titles, etc. if you expect them. Excel formulas do not support looping, so there are some limits to what you can do.

    0 讨论(0)
提交回复
热议问题