Extract the last substring from a cell

后端 未结 9 1855
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题