Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to split 'Firstname Lastname' into 2 columns 'First' and 'Last'?

I import a .csv file from PayPal that throws the customer's First name, MI and Last Name all in one column. I have no problem going through and deleting the middle initial to make just two words separated with a space.

How can I split the First and Last names that are separated by a space into two separate columns? It's a piece of cake in Excel, but for some stupid reason, I can't use Excel 08 for Mac to do it. Keeps saying that there is data in column #XX without a heading (there is no data there) when I try to import to PayPal Multi-order shipping.

Please help! I'm desperate. Taking me for ever to cut and past each last name into the new column.

Thanks for any help with this!

Marcus

iMac Quad Core i7, Mac OS X (10.6.6)

Posted on Feb 19, 2011 10:14 PM

Reply
Question marked as Best reply

Posted on Feb 20, 2011 12:12 AM

Voodoowho wrote:
I import a .csv file from PayPal that throws the customer's First name, MI and Last Name all in one column. I have no problem going through and deleting the middle initial to make just two words separated with a space.

How can I split the First and Last names that are separated by a space into two separate columns? It's a piece of cake in Excel, but for some stupid reason, I can't use Excel 08 for Mac to do it. Keeps saying that there is data in column #XX without a heading (there is no data there) when I try to import to PayPal Multi-order shipping.

Please help! I'm desperate. Taking me for ever to cut and past each last name into the new column.

Thanks for any help with this!

Marcus


Hi Marcus,

I'm assuming for the purpose of the formula that the names are in column A, starting at A2.
Insert two columns (which will become columns B and C) after the one containing the names.

In B2, enter =LEFT(A,FIND(" ",A,)-1)

In C2, enter =RIGHT(A,LEN(A)-FIND(" ",A,))

These will produce correct results for names where there is only a first name and a last name, separated by a single space.

Regards,
Barry
8 replies
Question marked as Best reply

Feb 20, 2011 12:12 AM in response to Voodoowho

Voodoowho wrote:
I import a .csv file from PayPal that throws the customer's First name, MI and Last Name all in one column. I have no problem going through and deleting the middle initial to make just two words separated with a space.

How can I split the First and Last names that are separated by a space into two separate columns? It's a piece of cake in Excel, but for some stupid reason, I can't use Excel 08 for Mac to do it. Keeps saying that there is data in column #XX without a heading (there is no data there) when I try to import to PayPal Multi-order shipping.

Please help! I'm desperate. Taking me for ever to cut and past each last name into the new column.

Thanks for any help with this!

Marcus


Hi Marcus,

I'm assuming for the purpose of the formula that the names are in column A, starting at A2.
Insert two columns (which will become columns B and C) after the one containing the names.

In B2, enter =LEFT(A,FIND(" ",A,)-1)

In C2, enter =RIGHT(A,LEN(A)-FIND(" ",A,))

These will produce correct results for names where there is only a first name and a last name, separated by a single space.

Regards,
Barry

Feb 23, 2011 1:20 AM in response to Voodoowho

Here is a script which does the trick.

--

--[SCRIPT splitfirst_lastname]
(*
Enregistrer le script en tant que Script ou Application : splitfirst_lastname.xxx
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.
Sélectionner la colonne de chaînes à découper (et éventuellement davantage pour définir la colonne recevant les noms propres).
Aller au menu Scripts , choisir Numbers puis choisir splitfirst_lastname
Le script découpe les chaînes sources au premier espace.
Le prénom remplace la chaîne initiale.
Le reste est déposé dans la cellule adjacente à droite ou dans la cellule de la denière colonne sélectionnée.
--=====
L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
Sous 10.6.x,
aller dans le panneau "Général" du dialogue Préférences de l'Éditeur Applescript
puis cocher la case "Afficher le menu des scripts dans la barre des menus".
--=====
Save the script as a Script or an Application : splitfirst_lastname.xxx
Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.
Select a column of strings to split (and maybe more columns to define the column receiving lastNames).
Go to the Scripts Menu, choose Numbers, then choose "splitfirst_lastname"
The script split the source strings on the first embedded space.
The FirstName replace the original string.
The reminder is stored in the cell adjacent on the right (or in the last column of the selected range.
--=====
The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
Under 10.6.x,
go to the General panel of AppleScript Editor’s Preferences dialog box
and check the “Show Script menu in menu bar” option.
--=====
Yvan KOENIG (VALLAURIS, France)
2011/02/23
*)
--=====
on run
run script doyourduty
end run
--=====
script doyourduty
set {dName, sName, tName, rname, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
if colNum2 = colNum1 then set colNum2 to colNum1 + 1
tell application "Numbers" to tell document dName to tell sheet sName to tell table tName
repeat with r from rowNum1 to rowNum2
tell row r
set first_last to value of cell colNum1
if (first_last is not 0.0) and first_last contains space then
set in_pieces to my decoupe(first_last, space)
set value of cell colNum1 to item 1 of in_pieces
set value of cell colNum2 to my recolle(items 2 thru -1 of in_pieces, space)
end if
end tell -- row
end repeat
end tell -- Numbers
end script
--=====
(*
set {rowNum1, colNum1, rowNum2, colNum2} to my getCellsAddresses(dname,s_name,t_name,arange)
*)
on getCellsAddresses(d_Name, s_Name, t_Name, r_Name)
local two_Names, row_Num1, col_Num1, row_Num2, col_Num2
tell application "Numbers"
set d_Name to name of document d_Name (* useful if we passed a number *)
tell document d_Name
set s_Name to name of sheet s_Name (* useful if we passed a number *)
tell sheet s_Name
set t_Name to name of table t_Name (* useful if we passed a number *)
end tell -- sheet
end tell -- document
end tell -- Numbers
if r_Name contains ":" then
set two_Names to my decoupe(r_Name, ":")
set {row_Num1, col_Num1} to my decipher(d_Name, s_Name, t_Name, item 1 of two_Names)
if item 2 of two_Names = item 1 of two_Names then
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
else
set {row_Num2, col_Num2} to my decipher(d_Name, s_Name, t_Name, item 2 of two_Names)
end if
else
set {row_Num1, col_Num1} to my decipher(d_Name, s_Name, t_Name, r_Name)
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
end if -- r_Name contains…
return {row_Num1, col_Num1, row_Num2, col_Num2}
end getCellsAddresses
--=====
(*
set { dName, sName, tName, rname, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
*)
on getSelParams()
local r_Name, t_Name, s_Name, d_Name
set {d_Name, s_Name, t_Name, r_Name} to my getSelection()

if r_Name is missing value then
if my parleAnglais() then
error "No selected cells"
else
error "Il n'y a pas de cellule sélectionnée !"
end if
end if

return {d_Name, s_Name, t_Name, r_Name} & my getCellsAddresses(d_Name, s_Name, t_Name, r_Name)
end getSelParams
--=====
(*
set {rowNumber, columnNumber} to my decipher(docName,sheetName,tableName,cellRef)
apply to named row or named column !
*)
on decipher(d, s, t, n)
tell application "Numbers" to tell document d to tell sheet s to tell table t to ¬
return {address of row of cell n, address of column of cell n}
end decipher
--=====
(*
set { d_Name, s_Name, t_Name, r_Name} to my getSelection()
*)
on getSelection()
local _, theRange, theTable, theSheet, theDoc, errMsg, errNum

tell application "Numbers" to tell document 1
repeat with i from 1 to the count of sheets
tell sheet i
set x to the count of tables
if x > 0 then
repeat with y from 1 to x
try
(selection range of table y) as text
on error errMsg number errNum
set {_, theRange, _, theTable, _, theSheet, _, theDoc} to my decoupe(errMsg, quote)
return {theDoc, theSheet, theTable, theRange}
end try
end repeat -- y
end if -- x>0
end tell -- sheet
end repeat -- i
end tell -- document
return {missing value, missing value, missing value, missing value}
end getSelection
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, t
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
--[/SCRIPT]
--


Yvan KOENIG (VALLAURIS, France) mercredi 23 février 2011 10:18:36

Feb 23, 2011 5:29 AM in response to Voodoowho

Voodoowho wrote:
Thanks a bunch Barry! Worked like a charm. I wonder if there would be a way to save this as a macro or something like that where i could just highlight the column with the names and click a button to split them out. *I'll never remember the formula for doing it*. Thanks again.

VDW,

You can save your working document as a Template to use whenever you need this feature.

Jerry

Feb 23, 2011 9:34 PM in response to KOENIG Yvan

It worked like a charm Yvan! Just as a note to help other (nubies) people who might be interested, under the Users::Library folder, I had to create the Scripts:Applications:Numbers: folders in which to save the new script. It also took me awhile to figure out that you were talking about displaying the "Script" menu in the AppleScript Editor. Once I figured that out, I also figured out that I could have my .csv file opened in Numbers with the column holding the data to be manipulated selected and, simultaneously, open AppleScript Editor, open the script in that program and run it there.

Sweet! I was clue-free about the AppleScript Editor and how that whole thing works. I just got a nice little lesson there. What's baffling it the amount of code that went into the script in order to do such a seemingly simple task.

I sure wish that I could figure it out so that I could make it do something cool like also handling a middle initial, or even creating the extra columns needed to a clean breakout of the data. Given the complexity (to me anyway) of the code, I'm guessing that I'll probably have to leave that to the experts like you Yvan. Thanks so much again my friend. Thanks to all of you helpers.

Marcus

Feb 24, 2011 7:23 AM in response to Voodoowho

Voodoowho wrote:
It worked like a charm Yvan! Just as a note to help other (nubies) people who might be interested, under the Users::Library folder, I had to create the Scripts:Applications:Numbers: folders in which to save the new script.


This is explained at the very beginning of the script in French (my main language) then in English (my third language). The 2nd one is AppleScript.

It also took me awhile to figure out that you were talking about displaying the "Script" menu in the AppleScript Editor. Once I figured that out, I also figured out that I could have my .csv file opened in Numbers with the column holding the data to be manipulated selected and, simultaneously, open AppleScript Editor, open the script in that program and run it there.

Sweet! I was clue-free about the AppleScript Editor and how that whole thing works. I just got a nice little lesson there. What's baffling it the amount of code that went into the script in order to do such a seemingly simple task.


The code used to split is really short.

What is long is the code used to grabbed the range of selected cells.

I sure wish that I could figure it out so that I could make it do something cool like also handling a middle initial, or even creating the extra columns needed to a clean breakout of the data. Given the complexity (to me anyway) of the code, I'm guessing that I'll probably have to leave that to the experts like you Yvan. Thanks so much again my friend. Thanks to all of you helpers.


The handlers used to grab the selected range may be reused.
I have about 30 or 40 scripts using them.

It would be interesting to put them in a shared library but here I send scripts dedicated to respond to a specific problem, not an enhanced set of tools.

Yvan KOENIG (VALLAURIS, France) jeudi 24 février 2011 16:23:30

May 17, 2013 6:36 PM in response to Voodoowho

Here is a formula to deal with a column that has first, middle, last names. If you only want the last name use the formula noted above (everything assumes cell A2 contains "Last, First Middle"): =LEFT(A2,SEARCH(" ",A2)-1), to get the first name use the nested formula: =IF(ISERROR(MID(A2,FIND(" ",A2),FIND(" ",A2,FIND(" ",A2)+1)-FIND(" ",A2))),RIGHT(A2,LEN(A2)+1-FIND(" ",A2)),MID(A2,FIND(" ",A2),FIND(" ",A2,FIND(" ",A2)+1)-FIND(" ",A2)))


The nested formula will eliminates the error if it sees only (Last, First) as opposed to (Last, First Middle).

How to split 'Firstname Lastname' into 2 columns 'First' and 'Last'?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.