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

Recommended Answers

All 2 Replies

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?

Using Array :

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

a = Split(strmsg, " ")
Debug.Print a(UBound(a))
commented: good example +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.