Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel Formula for Separating First and Last Name into separate columns

Posted on 2010-11-17
4
Medium Priority
?
800 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

705 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