Solved

Excel Formula for Separating First and Last Name into separate columns

Posted on 2010-11-17
4
759 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 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

21 Experts available now in Live!

Get 1:1 Help Now