0

I'm playing around on how to get last name in the string.
Below is my current code and it is working. I want to know if there is another way to do it.

    strMsg = "John Doe"
    length = Len(strMsg)

    For i = 0 To length
        char = Right(strMsg, i + 1)
            If Left(char, 1) = " " Then
                LastName = Trim(char)
                Debug.Print LastName
                Exit For
            End If
    Next i
2
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by Jx_Man
Featured Replies
  • 1

    Using Array : strmsg = "John Doe der" Dim a() As String a = Split(strmsg, " ") Debug.Print a(UBound(a)) Read More

0

Another way round to write this logic after did some 'Google'd

    strMsg = "John Doe"

    For i = 0 To Len(strMsg)
        char = Mid(strMsg, Len(strMsg) - (0 + i), Len(strMsg))
            If Left(char, 1) = " " Then
                LastName = Trim(char)
                Debug.Print LastName
                Exit For
            End If
    Next i

Any other ideas?

1

Using Array :

strmsg = "John Doe der"
Dim a() As String

a = Split(strmsg, " ")
Debug.Print a(UBound(a))
Comments
good example
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.