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

Splitting name string into columns

Hi,


I would like to be able to automatically convert name strings into separate columns for 'Title', 'First Name' and 'Last Name'. The name strings are in one of the following two formats:


Professor Nobby Stiles

Britney Spears


So the formulas would need to do something like this:


1. Check if there are two or three words in the string


2. If three words are found, add the words to each of the three columns in sequence, i.e.:


StringTitleFirst NameLast Name
Professor Nobby StilesProfessorNobbyStiles


3. If two words are found, skip the 'Title' column and add the words to the last two columns in sequence, i.e.:


StringTitleFirst NameLast Name
Britney SpearsBritneySpears


Finally, the values in the string are actually all lowercase (I have used Format->Font->Capitalisation->Title to change them), and I would like the values to be converted to Title case so that when exporting as a CSV file the columns are all formatted as title case.


I would be glad of any help to automate this.


Thanks,


Nick

Posted on Sep 21, 2013 5:51 AM

Reply
Question marked as Best reply

Posted on Sep 21, 2013 12:46 PM

Hi Nick,


Provided all of your names strictly follow the pattern shown in your two examples, the separation is not all that difficult. The process will fail, though, with names such as James Earl Jones.


Ensuring each part begins with a capital adds complexity to the formula. Here, I've done that as a separate step. The formula in columns F, G and H can be combined with those in D, E and F if desired.

User uploaded file

Formulas:


Column B is an auxiliary column. Its results are used in the formulas in the next three columns.


B2, and filled down: =LEN(A)-LEN(SUBSTITUTE(A," ","",))


This counts the number of spaces in the string in Column A.


C2 and filled down: =IF(B<2,"",LEFT(A,FIND(" ",A)-1))


IF returns a null string if the number of spaces in the string in A is less than 2. Otherwise, it passes control to LEFT (and FIND).

Find returns the position of the first space in the string in column A. 1 is subtracted to give the position of the last character to be included in the string placed in this column. LEFT returns the characters the first word.


D2, and filled down: =IF(B<2,LEFT(A,FIND(" ",A)-1),MID(A,FIND(" ",A)+1,FIND(" ",A,FIND(" ",A)+1)-FIND(" ",A)))


IF there are less that two spaces in the string, the 'else' part of the formula above is called to return everything before the first space.

*Note that this will return an error if there are no spaces in the string (eg. Beyoncé, or Sting). See below.

If there are two (or more) spaces, MID returns everything between the first and second space, using the resuts of the first FIND (following MID) to determine where to start, and the results of the second and third FINDs to detrmine how many characters to include.


E2, and filled down: =RIGHT(A,LEN(A)-(B+LEN(C&D))+(B-1))


This returns all characters to the right of the second space in the string in column A


*This will aso return an error if there are no spaces in column A.


Errors:


The two errors above can be trapped using IFERROR.


To do so, enclose each of the formulas with IFERROR, as below:


D2: =IFERROR(IF(B<2,LEFT(A,FIND(" ",A)-1),MID(A,FIND(" ",A)+1,FIND(" ",A,FIND(" ",A)+1)-FIND(" ",A))),"")

E2: =IFERROR(RIGHT(A,LEN(A)-(B+LEN(C&D))+(B-1)),"")


Capitalisation:


Formatting the cells as "Title" displays the initial etter of each word as a capital, but does not change the actual data in the cell. To do that, you'll need to use the UPPER() function.


In the example table, this is done in columns F, G and H, using the data in columns D, E and F respectively.


F2: =IF(LEN(C)>0,UPPER(LEFT(C,1))&RIGHT(C,LEN(C)-1),"")


This formula may be filled right to column H, and filed down each column.


Regards,

Barry

10 replies
Question marked as Best reply

Sep 21, 2013 12:46 PM in response to nick_harambee

Hi Nick,


Provided all of your names strictly follow the pattern shown in your two examples, the separation is not all that difficult. The process will fail, though, with names such as James Earl Jones.


Ensuring each part begins with a capital adds complexity to the formula. Here, I've done that as a separate step. The formula in columns F, G and H can be combined with those in D, E and F if desired.

User uploaded file

Formulas:


Column B is an auxiliary column. Its results are used in the formulas in the next three columns.


B2, and filled down: =LEN(A)-LEN(SUBSTITUTE(A," ","",))


This counts the number of spaces in the string in Column A.


C2 and filled down: =IF(B<2,"",LEFT(A,FIND(" ",A)-1))


IF returns a null string if the number of spaces in the string in A is less than 2. Otherwise, it passes control to LEFT (and FIND).

Find returns the position of the first space in the string in column A. 1 is subtracted to give the position of the last character to be included in the string placed in this column. LEFT returns the characters the first word.


D2, and filled down: =IF(B<2,LEFT(A,FIND(" ",A)-1),MID(A,FIND(" ",A)+1,FIND(" ",A,FIND(" ",A)+1)-FIND(" ",A)))


IF there are less that two spaces in the string, the 'else' part of the formula above is called to return everything before the first space.

*Note that this will return an error if there are no spaces in the string (eg. Beyoncé, or Sting). See below.

If there are two (or more) spaces, MID returns everything between the first and second space, using the resuts of the first FIND (following MID) to determine where to start, and the results of the second and third FINDs to detrmine how many characters to include.


E2, and filled down: =RIGHT(A,LEN(A)-(B+LEN(C&D))+(B-1))


This returns all characters to the right of the second space in the string in column A


*This will aso return an error if there are no spaces in column A.


Errors:


The two errors above can be trapped using IFERROR.


To do so, enclose each of the formulas with IFERROR, as below:


D2: =IFERROR(IF(B<2,LEFT(A,FIND(" ",A)-1),MID(A,FIND(" ",A)+1,FIND(" ",A,FIND(" ",A)+1)-FIND(" ",A))),"")

E2: =IFERROR(RIGHT(A,LEN(A)-(B+LEN(C&D))+(B-1)),"")


Capitalisation:


Formatting the cells as "Title" displays the initial etter of each word as a capital, but does not change the actual data in the cell. To do that, you'll need to use the UPPER() function.


In the example table, this is done in columns F, G and H, using the data in columns D, E and F respectively.


F2: =IF(LEN(C)>0,UPPER(LEFT(C,1))&RIGHT(C,LEN(C)-1),"")


This formula may be filled right to column H, and filed down each column.


Regards,

Barry

Sep 22, 2013 12:49 PM in response to nick_harambee

"The error message is "Argument 4 in SUBSTITUTE must be greater than or equal to 1""


My bad, Nick. In the formula bar, the formula looks like this:

User uploaded file

and works just fine. But I forgot that when copied, then pasted into a text environment, the fina comma must be removed.


@Jerry: Thanks for stepping in and making the correction.


Regarding combining the formulas to acheve the capitalisation with the ones extracting each part of the name:


Here's an example, using the formulas in C2 and F2.


C2: =IF(B<2,"",LEFT(A,FIND(" ",A)-1))

F2: =IF(LEN(C)>0,UPPER(LEFT(C,1))&RIGHT(C,LEN(C)-1),"")


In C2, there are two possible results: if-TRUE: "", if-FALSE: LEFT(A,FIND(" ",A)-1)

Only the if-FALSE result requires capitalisation; if-TRUE, a null string, has no characters, so there is nothing to capitalise.

We can combine the capitalisation with the if-FALSE part of this formula.


Looking at F2: We can eliminate the condition in F2's formula, as well as the "" result if that condition is FALSE. Since we are combining this with the if-FALSE part of C2's formula, 'C' will always have some length.


So here's step 1 of the rewrite:


C2: IF(B<2,"",UPPER(LEFT(C,1))&RIGHT(C,LEN(C)-1))


All that's left to do is replace each occurence of "C" with the if-FALSE calculation from the original formula in C2:


C2: IF(B<2,"",UPPER(LEFT(LEFT(A,FIND(" ",A)-1),1))&RIGHT(LEFT(A,FIND(" ",A)-1),LEN(LEFT(A,FIND(" ",A)-1))-1))



For the other two formulas, the revision process is much the same: Determine what part of the first formula always requires applying capitalisation, replace that part with the action part of the capitalising formula " PPER(LEFT(C,1))&RIGHT(C,LEN(C)-1) ", then insert what you've replaced in place of the three Cs (or Ds or Es) in UPPER(LEFT(C,1))&RIGHT(C,LEN(C)-1).


Personally, I'd stick with the solution offered above, using columns B, C, D and E as auxiliary columns (which could be hidden), and F, G and H to contain the final results:

User uploaded file

Regards,

Barry

Splitting name string into columns

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