Solved

Excel Formula for Separating First and Last Name into separate columns

Posted on 2010-11-17
4
745 Views
Last Modified: 2012-08-13
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
Comment
Question by:Rowby Goren
4 Comments
 
LVL 50

Accepted Solution

by:
barry houdini earned 350 total points
ID: 34159039
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
 
LVL 20

Assisted Solution

by:pari123
pari123 earned 75 total points
ID: 34159173
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
 
LVL 2

Assisted Solution

by:JamesTX
JamesTX earned 75 total points
ID: 34159282
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
 
LVL 9

Author Closing Comment

by:Rowby Goren
ID: 34159474
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now