• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Excel Formula for Separating First and Last Name into separate columns

HI

I have a spreadsheet with one column in the following format:

DOE, JOHN   (lastname, firstname)

Can someone give me a formula for separating the first and lastname into two columns.

Thanks!


Rowby

0
Rowby Goren
Asked:
Rowby Goren
3 Solutions
 
barry houdiniCommented:
You could use Text to columns with comma as delimiter or use these formulas in B1 and C1 assuming your data is in A1

=LEFT(A1,FIND(",",A1)-1)

=TRIM(REPLACE(A1,1,FIND(",",A1),""))

regards, barry
0
 
Ardhendu SarangiSr. Project ManagerCommented:
Here's 1 way to do it...

Assuming all your names are separated by comma's, you can use Excel Text To Columns option too.

1. Select the column with the names in it.

2. Click on Data from the Menu bar, select Text to Columns.

3. Select "Delimited" option on the pop-up page and click next.

4. Check "comma" as the delimiter in the next page and then click Finish.

And you should be done!

Remember this will only work if the names are separated by comma's and there is an empty column to the right of the name column.

thanks.
Ardhendu
0
 
JamesTXCommented:
If you know how to put macros into your personal add-in, you could use the functions from the code below. I wrote it a couple years ago (so it's probably a little bloated), but it gets the job done most of the time. It will handle a few standard name forms and all you have to put in for the function is:

First Name: =fname(cell)
Last Name: =lname(cell)
Middle Name: = mname(cell)

It will handle formats like John Doe; John F Doe; Doe, John; Doe, John F; and maybe more. Anyway, it should work. Happy hunting!
Function LName(Given_Data As Range)

'created by JamesTX - 3/27/2008
'This function pulls the last name out of a given string

    Dim HasComma, HasPeriod, HasSpace As Boolean
    Dim AppWSF As Object
    Dim FullTxt, Field1, Field2, Field3, Field4 As String
    Dim CurrLetter, NameArray(99) As String, c As Variant
    Dim CountSpace, GivenLen, CommaLoc, PeriodLoc, SpaceLoc, counter As Integer
    
    counter = 0
    CountSpace = 0
    GivenLen = VBA.Len(Given_Data.Value)
    FullTxt = Given_Data.Value
    Set AppWSF = Application.WorksheetFunction
    
    For Each c In NameArray
        If counter < GivenLen Then
            CurrLetter = VBA.Mid(FullTxt, counter + 1, 1)
            NameArray(counter) = CurrLetter
            If CurrLetter = " " Then
                HasSpace = True
                SpaceLoc = AppWSF.Find(" ", FullTxt, 1)
            End If
            If CurrLetter = "," Then
                HasComma = True
                CommaLoc = AppWSF.Find(",", FullTxt, 1)
            End If
            If CurrLetter = "." Then
                HasPeriod = True
                PeriodLoc = AppWSF.Find(".", FullTxt, 1)
            End If
        Else
            Exit For
        End If
        counter = counter + 1
    Next c
    LName = NameArray(1) & NameArray(2)
  
    If HasSpace = True Then
        CountSpace = GivenLen - VBA.Len(VBA.Replace(FullTxt, " ", ""))
        LName = CountSpace
    Else
        LName = "INVALID STRING"
    End If

    Select Case CountSpace
        Case 1
            If HasComma = True Then
                LName = VBA.Left(FullTxt, CommaLoc - 1)
            ElseIf HasPeriod = True Then
                If PeriodLoc = 2 Then
                    LName = VBA.Right(FullTxt, VBA.Len(FullTxt) - 3)
                Else
                    LName = VBA.Mid(FullTxt, VBA.Len(FullTxt) - 1, 1)
                End If
            Else
                LName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc)
            End If
        Case 2
            Dim SpaceLoc2 As Integer
            SpaceLoc2 = AppWSF.Find(" ", FullTxt, SpaceLoc + 1)
            
            If HasComma = True Then
                LName = VBA.Left(FullTxt, CommaLoc - 1)
            ElseIf HasPeriod = True Then
                If PeriodLoc = 2 Then
                    If VBA.Right(FullTxt, 1) = "." Then
                        LName = VBA.Mid(FullTxt, VBA.Len(FullTxt) - 1, 1)
                    Else
                        LName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc2)
                    End If
                Else
                    If VBA.Right(FullTxt, 1) = "." Then
                        LName = VBA.Mid(FullTxt, VBA.Len(FullTxt) - 1, 1)
                    Else
                        LName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc2)
                    End If
                End If
            
            Else
                LName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc2)
            End If
        Case Else
            LName = "INVALID STRING"
    End Select
    
    If VBA.Len(LName) = 1 Then LName = "ERROR"
    
End Function

Function FName(Given_Data As Range)

'created by JamesTX - 3/27/2008
'This function pulls the first name out of a given string

    Dim HasComma, HasPeriod, HasSpace As Boolean
    Dim AppWSF As Object
    Dim FullTxt, Field1, Field2, Field3, Field4 As String
    Dim CurrLetter, NameArray(99) As String, c As Variant
    Dim CountSpace, GivenLen, CommaLoc, PeriodLoc, SpaceLoc, counter As Integer
    
    counter = 0
    CountSpace = 0
    GivenLen = VBA.Len(Given_Data.Value)
    FullTxt = Given_Data.Value
    Set AppWSF = Application.WorksheetFunction
    
    For Each c In NameArray
        If counter < GivenLen Then
            CurrLetter = VBA.Mid(FullTxt, counter + 1, 1)
            NameArray(counter) = CurrLetter
            If CurrLetter = " " Then
                HasSpace = True
                SpaceLoc = AppWSF.Find(" ", FullTxt, 1)
            End If
            If CurrLetter = "," Then
                HasComma = True
                CommaLoc = AppWSF.Find(",", FullTxt, 1)
            End If
            If CurrLetter = "." Then
                HasPeriod = True
                PeriodLoc = AppWSF.Find(".", FullTxt, 1)
            End If
        Else
            Exit For
        End If
        counter = counter + 1
    Next c
    FName = NameArray(1) & NameArray(2)
  
    If HasSpace = True Then
        CountSpace = GivenLen - VBA.Len(VBA.Replace(FullTxt, " ", ""))
        FName = CountSpace
    Else
        FName = "INVALID STRING"
    End If

    Select Case CountSpace
        Case 1
            If HasComma = True Then
                FName = VBA.Right(FullTxt, GivenLen - SpaceLoc)
            ElseIf HasPeriod = True Then
                If PeriodLoc = 2 Then
                    FName = VBA.Left(FullTxt, 1)
                Else
                    FName = VBA.Left(FullTxt, SpaceLoc - 1)
                End If
            Else
                FName = VBA.Left(FullTxt, SpaceLoc - 1)
            End If
        Case 2
            Dim SpaceLoc2 As Integer
            SpaceLoc2 = AppWSF.Find(" ", FullTxt, SpaceLoc + 1)
            
            If HasComma = True Then
                If CommaLoc + 1 = SpaceLoc2 Then
                    FName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc2)
                Else
                    FName = VBA.Mid(FullTxt, CommaLoc + 2, SpaceLoc2 - (CommaLoc + 2))
                End If
            ElseIf HasPeriod = True Then
                If PeriodLoc = 2 Then
                    FName = VBA.Left(FullTxt, 1)
                Else
                    FName = VBA.Left(FullTxt, SpaceLoc - 1)
                End If
            
            Else
                FName = VBA.Left(FullTxt, SpaceLoc - 1)
            End If
        Case Else
            FName = "INVALID STRING"
    End Select

End Function

Function MName(Given_Data As Range)

'created by JamesTX - 3/27/2008
'This function pulls the middle name out of a given string

    Dim HasComma, HasPeriod, HasSpace As Boolean
    Dim AppWSF As Object
    Dim FullTxt, Field1, Field2, Field3, Field4 As String
    Dim CurrLetter, NameArray(99) As String, c As Variant
    Dim CountSpace, GivenLen, CommaLoc, PeriodLoc, SpaceLoc, counter As Integer
    
    counter = 0
    CountSpace = 0
    GivenLen = VBA.Len(Given_Data.Value)
    FullTxt = Given_Data.Value
    Set AppWSF = Application.WorksheetFunction
    
    For Each c In NameArray
        If counter < GivenLen Then
            CurrLetter = VBA.Mid(FullTxt, counter + 1, 1)
            NameArray(counter) = CurrLetter
            If CurrLetter = " " Then
                HasSpace = True
                SpaceLoc = AppWSF.Find(" ", FullTxt, 1)
            End If
            If CurrLetter = "," Then
                HasComma = True
                CommaLoc = AppWSF.Find(",", FullTxt, 1)
            End If
            If CurrLetter = "." Then
                HasPeriod = True
                PeriodLoc = AppWSF.Find(".", FullTxt, 1)
            End If
        Else
            Exit For
        End If
        counter = counter + 1
    Next c
    MName = NameArray(1) & NameArray(2)
    
    If HasSpace = True Then
        CountSpace = GivenLen - VBA.Len(VBA.Replace(FullTxt, " ", ""))
        MName = CountSpace
    Else
        MName = "INVALID STRING"
    End If
    Select Case CountSpace
        Case 1
            MName = ""
        Case 2
            Dim SpaceLoc2 As Integer
            SpaceLoc2 = AppWSF.Find(" ", FullTxt, SpaceLoc + 1)
            
            If HasComma = True Then
                If VBA.Right(FullTxt, 1) = "." Then
                    MName = VBA.Mid(FullTxt, VBA.Len(FullTxt) - 1, 1)
                ElseIf CommaLoc + 1 = SpaceLoc2 Then
                    MName = ""
                Else
                    MName = VBA.Right(FullTxt, VBA.Len(FullTxt) - SpaceLoc2)
                End If
            Else
                If VBA.Mid(FullTxt, SpaceLoc2 - 1, 1) = "." Then
                    MName = VBA.Mid(FullTxt, SpaceLoc + 1, 1)
                Else
                    MName = VBA.Mid(FullTxt, SpaceLoc + 1, SpaceLoc2 - (SpaceLoc + 1))
                End If
            End If
        Case Else
            MName = "INVALID STRING"
    End Select
 
End Function

Open in new window

0
 
Rowby GorenAuthor Commented:
THanks for the solution, I will keep them all for future.

I used text to columns with comma.  That was a new one for me.

I gave Barry the most points since he got in first :)  
But thanks to all!

Rowby
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now