Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Formula for Separating First and Last Name into separate columns

Posted on 2010-11-17
4
Medium Priority
?
810 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 1400 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:Ardhendu Sarangi
Ardhendu Sarangi earned 300 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

926 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