Excel split cells with full name into 2 columns.

Golaidron

Board Regular
Joined
Jan 23, 2013
Messages
83
Office Version
  1. 365
Platform
  1. Windows
Hi All

Need help her to find some formulas til split up a cell and into 2 cells.
Got thousands of row to edit.

Data i got in column A is set up randomly,

Lastname, Firstname
Lastname, Firstname Middelname
Lastname, Firstname Middelname Middelname

Firstname Lastname
Firstname Middelname Lastname
Firstname Middelname Middelname Lastname

Preferred outcome is
Column B to contain First+middelname
Column C to contain only lastname

Please help.
 
something like that?

Column1Column1.1Column1.2Column1.3LastnameNames
Mike, John AndrewMike,JohnAndrewMike,John Andrew
John Mike AndrewJohnMikeAndrewAndrewJohn Mike
Olsen, Bjern VitoOlsen,BjernVitoOlsen,Bjern Vito
Jon Rune HansenJonRuneHansenHansenJon Rune
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Every name before comma is the last name, rest is first and middlename.

If the name don't have comma, then the last name is lastname.



I have come up with a formula to generate the last name based on the criteria above. It requires the use of 2 UDF as follows:

Code:
Function EXTRACTELEMENT(txt, n, sep)
    EXTRACTELEMENT = Split(Application.Trim(txt), sep)(n - 1)
End Function

Code:
Function COUNTSPACES(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\S"
        COUNTSPACES = Len(.Replace(txt, ""))
    End With
End Function

Then try using this formula:

IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " "))

Where B2 contains the cell with the entire name - put this into C2 and it should output the surname.

It will contain a comma if a comma is present in B2, but you can do a find and replace on this at the end after turning it to a value instead of formula.

Now that you have C2 as the surname, do the following in cell D2:

TRIM(REPLACE(B2,SEARCH(C2,B2),LEN(C2),""))

This should output remaining names in to cell D2 which should be all but the surname, then you can re-adjust columns as desired.
 
Last edited:
Upvote 0
this is cosmetic :)

Column1Column1.1Column1.2Column1.3LastnameNames
Mike, John AndrewMike,JohnAndrewMikeJohn Andrew
John Mike AndrewJohnMikeAndrewAndrewJohn Mike
Olsen, Bjern VitoOlsen,BjernVitoOlsenBjern Vito
Jon Rune HansenJonRuneHansenHansenJon Rune

now I would like to know : do you have PowerQuery (Get&Transform) ?
and maximum words in one record
 
Last edited:
Upvote 0
Pity, so use any solution from others

have a nice day

btw. PowerQuery add-in for Excel 2010/2013, built-in for Excel 2016 and above (aka Get&Transform)
 
Last edited:
Upvote 0
If you want to get rid of the commas all in one go I have come up with a formula to do so which is pretty messy!

Remember that EXTRACTELEMENT and COUNTSPACES must be defined from my earlier reply.

ABCD
2Jamesson, Adam Mark TerryJamesonAdam Mark Terry
3Peter Richard ClarkClarkPeter Richard
4Thatcher, Liz AmyThatcherLiz Amy

<tbody>
</tbody>

This is the result of the below formula. Which just needs dragging down the C & D columns.

ABCD
2Jameson, Adam Mark TerryIFERROR(IF(SEARCH(",",IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " "))),LEFT(IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " ")),LEN(IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " ")))-1),IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " "))),IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " ")))IFERROR(IF(SEARCH(",",B2),MID(TRIM(REPLACE(B2,SEARCH(C2,B2),LEN(C2),"")),3,100),""),TRIM(REPLACE(B2,SEARCH(C2,B2),LEN(C2),"")))

<tbody>
</tbody>
 
Last edited:
Upvote 0
Thanks for all the help guys and girls.
Will try your last post tyija1995, just need to translate it to norwegian excel
 
Upvote 0
Hope it works Golaidron.

I have modified the formula so it is a lot shorter now. With full name in Cell B2, try these formulas in cells C2 and D2:

C2:
SUBSTITUTE(IFERROR(IF(SEARCH(",",B2),EXTRACTELEMENT(B2,1," "),""),EXTRACTELEMENT(B2,COUNTSPACES(B2)+1, " ")),",","")

D2:
SUBSTITUTE(TRIM(REPLACE(B2,SEARCH(C2,B2),LEN(C2),"")),", ","")

Substitutes out the comma with nothing - should work the same as the lengthy formula i posted before.
 
Upvote 0
Hi

It sort of work.

Extractelement and Countspaces are not in the Norwegian Excel, can't find any translation on google either.

Thanks for all you help tyija1995
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top