How To Use The VBA Trim And Split Functions To Parse Names

In column A we have a list of random names

Our task is to divide the list of names into first and last names

We are going to parse (divide) the names by using the VBA “split” function. Then we will use the VBA Trim function to remove the excess spaces.

We will use a custom vba function to facilitate this task:

Remember that customized functions need to be placed in modules!

Function GetFName(WholeName) As String
    Dim strTemp As String
    Dim varParts As Variant
        
    varParts = Split(WholeName, " ")
    
    strTemp = varParts(0)
    
    strTemp = Trim(strTemp)

    GetFName = strTemp
    
End Function


Function GetLName(WholeName) As String
    Dim strTemp As String
    Dim varParts As Variant
    
    varParts = Split(WholeName, " ")
    
    strTemp = varParts(1)
    strTemp = Trim(strTemp)
    
    GetLName = strTemp
    
End Function

Now watch how it’s done:

Let me know if you have any questions.

****************************************************


 


Posted

in

by

Tags: