Parsing out First and Last name from Excel field - excel

I have a field (column) in excel in the format of "LastName, FirstName MiddleInitial" with a space between the comma after the last name and the first name and a second space between the middle initial and the first name (no comma after the first name). Is there a way to identify which cells have a middle initial on the right hand side and then eliminate the middle initial for all cells such that the output will look like "LastName, FirstName"?
Thanks!

What you want to do is to be able to parse the field into multiple fields, and then rejoin then with a simple excel formula. To take a field and put it into multiple columns you can use the following approach for "Text to Columns"
Text to Columns
Once you have 3 columns of text, you can group the first two together by using a formula like =A1&", "&B1 in the column you want to have the value. (In my example, the last name would be in A1, and first name in B1)

You could also do it as a cell formula without changing your original data. I built this up using the models presented on How to split a string based on “:” in MS-Excel?, over at SuperUser.
Assuming you have a name (Public, John Q) in a1:
=LEFT(A1,FIND(" ",A1)) & LEFT(MID(A1,FIND(" ",A1)+1,LEN(A1)),IFERROR(FIND(" ",MID(A1,FIND(" ",A1)+1,LEN(A1)))-1,LEN(MID(A1,FIND(" ",A1)+1,LEN(A1)))))
It looks very complicated, but really isn't:
Using these two components:
Get everything to the left of the first space in a string: LEFT(A1,FIND(" ",A1))
Get everything to the right of the first space in a string: MID(A1,FIND(" ",A1)+1,LEN(A1)))
We can grab these pieces:
Get the last name: LEFT(A1,FIND(",",A1)+1)
Get the first name and middle name: MID(A1,FIND(" ",A1)+1,LEN(A1)))
Then recurse by putting the "left of a space" construction around the "right of a string" construction:
MID( LEFT(A1,FIND(",",A1)+1) ,FIND(" ", LEFT(A1,FIND(",",A1)+1) )+1,LEN( LEFT(A1,FIND(",",A1)+1) )))
Unless there is no second space (when there's no middle name.) That's what the Iferror is for - in that case, you want the entire length of what you got after the first space:
LEFT(MID(A1,FIND(" ",A1)+1,LEN(A1)),IFERROR(FIND(" ",MID(A1,FIND(" ",A1)+1,LEN(A1)))-1,LEN(MID(A1,FIND(" ",A1)+1,LEN(A1)))))
Put them together for lastname, firstname:
=LEFT(A1,FIND(" ",A1)) & LEFT(MID(A1,FIND(" ",A1)+1,LEN(A1)),IFERROR(FIND(" ",MID(A1,FIND(" ",A1)+1,LEN(A1)))-1,LEN(MID(A1,FIND(" ",A1)+1,LEN(A1)))))

Related

Splitting names given in single cells without using text to column excel

i want to use excel formula to split multiple names given in a single cell. dont want to use text to column feature. For example
in the above yellow is the variable name & the green color is the required format
See find the nth instance of a character: FIND(CHAR(1),SUBSTITUTE(string,delimiter,CHAR(1),nth)). For the first, we use LEFT(string,position_first-1). For the last: RIGHT(string,LEN(string)-position_last). For all in between: MID(string,position_first+1,position_second-position_first-1).
So, combining the logic, we may get this:
=IFERROR(IFERROR(IF(B$1=1,LEFT($A2,FIND(CHAR(1),SUBSTITUTE($A2,"/",CHAR(1),1),1)-1),MID($A2,FIND(CHAR(160),SUBSTITUTE($A2,"/",CHAR(160),B$1-1),1)+1,FIND(CHAR(1),SUBSTITUTE($A2,"/",CHAR(1),1+B$1-1),1)-FIND(CHAR(1),SUBSTITUTE($A2,"/",CHAR(1),B$1-1),1)-1)),RIGHT($A2,LEN($A2)-FIND(CHAR(1),SUBSTITUTE($A2,"/",CHAR(1),B$1-1),1))),"")
IFERROR(...,"") is used to return "" after last occurrence (below, in G2). Nested IFERROR(... RIGHT) will be triggered at last occurrence (since MID will fail there; below at F2).
Try using this:
With the full name in cell A2, the formulas go as follows:
Get the first name:
=LEFT(A2,SEARCH(" ",A2)-1)
Get the last name:
=RIGHT(A2,LEN(A2)-SEARCH(" ",A2,1))
You enter the formulas in cells B2 and C2, respectively, and drag the fill handle to copy the formulas down the columns. The result will look something similar to this:

Extract the last but one substring from a cell

I have addresses in a column. I need to split just the last but one name from that column into another column.
The last but one substring is delimited by a space from both the right and left sides.
The content in cell A2 is "via Milano, 25 ROMA RM" and I entered the below formula in cell B2 (I need "ROMA" in cell B2)
I tried using the following formula:
=MID(A2,FIND(CHAR(1),SUBSTITUTE(A2," ",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))+1,LEN(A2))
But it returns the last substring from a given string (in this case "RM"). Can't manage to modify it to get the last but one substring. If useful: currently using macOS Catalina 10.15.7.
Edit: the string address is ALWAYS formatted in the same way "via", "street name","house number", "comune", "province".
You could do something like the hideous:
=LEFT(
RIGHT(
SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1),
LEN(SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1))-
SEARCH("!", SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1))),
SEARCH(" ",
RIGHT(SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1),
LEN(SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1))-
SEARCH("!", SUBSTITUTE(A2," ","!",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1)))))
though I am pretty sure there are faster/cleaner ways to get what you want. Note that "!" was just chosen as a character that wouldn't otherwise appear in the string. Also note this is why best practice is to store the smallest pieces of data as separate items rather than in one string!
REQUESTED EDIT: If you are just looking to get everything to the right of a comma and to the left of a space (e.g. "25" from "Via Milano, 25 ROMA RM") you can use something like the below
=LEFT(
TRIM(RIGHT(A2,LEN(A2)-SEARCH(",",A2))),
SEARCH(" ", TRIM(RIGHT(A2,LEN(A2)-SEARCH(",",A2)))))

Find first and second match of a list in Excel

I have text cells in column and i have a list of words, i need to get the first and second match of text cell within this list of word using formula without vba.
here is an example of the result
If your text cells start in A2, then:
First Match B2: =IFERROR(MID($A2,AGGREGATE(15,6,SEARCH(LIST,$A2),COLUMNS($A:A)),FIND(" ",$A2&" ",AGGREGATE(15,6,SEARCH(LIST,$A2),COLUMNS($A:A)))-AGGREGATE(15,6,SEARCH(LIST,$A2),COLUMNS($A:A))),"")
and fill right one cell to get the 2nd Match. Then fill down as far as needed.
EDIT: The OP has added an additional requirement having to do with excluding words within words, eg do NOT find also if the word is Calso; and also do not return punctuation.
Although cumbersome in formulas, this can be handling by
- Replacing all of the punctuation with space
- Adding a space at the beginning and end of the sentence
- Adding a space at the beginning and end of each word in LIST
- adjusting the formula to not return the extra space.
The above can be done most simply by modifying the defined name LIST and also by using a defined name for a formula to do the punctuation replacement and space prefix and suffix.
Given the example above, we redefine LIST
LIST refers to: =" " & Sheet1!$F$2:$F$6 & " "
and, with some cell in Row 2 selected, we define theSentence
theSentence refers to: =" " & TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Sheet1!$A10,","," "),"'"," "),"."," "),"!"," "))& " "
That particular definition will remove comma, apostrophe, period and exclamation point. If you need to remove other punctuation, you can just nest more SUBSTITUTE's
And we make some changes in the formula in B2:
B2: =IFERROR(MID(theSentence,1+AGGREGATE(15,6,SEARCH(LIST,theSentence),COLUMNS($A:A)),FIND(" ",theSentence,1+AGGREGATE(15,6,SEARCH(LIST,theSentence),COLUMNS($A:A)))-AGGREGATE(15,6,SEARCH(LIST,theSentence),COLUMNS($A:A))-1),"")
This is the second interpretation of the question (find the first and second match in the list of a string anywhere in the text).
=IFERROR(INDEX($F$2:$F$6,SMALL(IF(ISNUMBER(FIND(" "&$F$2:$F$6&" "," "&$A2&" ")),ROW($F$2:$F$6)-ROW($F$1)),COLUMNS($A1:A1))),"")
Note that this is an array formula and must be entered with CtrlShiftEnter

Creating a space in between values in Excel spreadsheet

There are two types of values in cell A of my spreadsheet
Value type 1: Have a space in between the postcode district and sub postcode, as the postcode district is less than ten (i.e. MK1-MK9)
MK1 1AS
Value type 2: Have no space in between, as the postcode district is greater than ten (i.e. MK10-MK46)
MK170DB
What would be the best way of splitting the second group of values into something like this:
MK17 0DB
I was thinking of some pseudo code in the the vein of:
if the value at the 4th character (counting from the right) in MK170DB is not an empty space
then count 4 spaces and create an empty character, leaving it like this MK17 0DB
if not then presume that the 4th character is a empty space (i.e. MK1 1AS) and leave it
As I just need to run this operation once to cleanse my data, I was thinking about creating a formula in column B that references column A and does the necessary cleansing. I would then replace the values in col A with what I have in col B.
Can anyone tell me whether the logic I have proposed can be executed in Excel or if there is a better way of doing it?
Thanks.
With your data in A1, in B1 enter:
=IF(ISERROR(FIND(" ",A1)),LEFT(A1,4) & " " & MID(A1,5,999),A1)
Something I came up with, which appears to work...
=IF(MID(A1,4, 1)=" ",A1,REPLACE(A1,5,0," "))

How can I switch a string from "lastName, firstName" to "firstName LastName"?

I have a column full of names written as:
"lastName, firstName"
I want to have a nother column that has this name list written as:
"firstName LastName"
So, how can I switch a string from "lastName, firstName" to "firstName LastName" ?
If the first name is in A2 try this formula in B2 copied down
=MID(A2&" "&A2,FIND(" ",A2)+1,LEN(A2)-1)
Enter data into cells e.g.
Brown, John
Green, Bob
Smith, Will
(Note that the comma in this case is the delimiter which the system will use to separate the entries)
Highlight these cells.
Click on the "Data" tab,
click on "Text to Column".
Choose options offered.
The worked for me
=RIGHT(C3,LEN(C3)-LEN(LEFT(C3,FIND(",",C3)-1))-1) & " " & LEFT(C3,FIND(",",C3)-1)
Barry's answer is correct, if the single cell (A2) doesn't ALSO contain errors like "firstname lastname" -- as is often the case.
So, to deal with the cell data being either "Last, First" or "First Last", we'll need to compare the cell and take action appropriately. Also note, in my example, I moved the first name to it's own column, and last name to it's own column. You can of course join them into a single column.
First, let's break down what we need. This will return the first name when there is a "," in the cell contents: (assumes the field you are wanting to split is in A2, adjust as needed)
=MID(A2;FIND(",";A2)+2;99)
and this will return the first name when there isn't:
=MID(A2;1;FIND(" ";A2))
And for Last name when there is a comma:
=MID(A2;1;FIND(",";A2)-1)
and last name when there isn't:
=MID(A2;FIND(" ";A2)+1;99)
So, we need to test if there is a "," in the string, and deal with an error condition when there isn't (FIND returns #value when not found, ISERROR() can test for that). Optionally (and my approach) was to check if the return of the FIND yielded a number (an error won't). In this case, an "error condition of not finding it" would yield a non-number #Value.
=IF( ISNUMBER( FIND(",";A2)); MID(A2;FIND(",";A2)+2;99); MID(A2;1;FIND(" ";A2)))
Doing the same thing for the Last name looks like this:
=IF( ISNUMBER( FIND(","; A2)); MID(A2;1;FIND(",";A2)-1); MID(A2;FIND(" ";A2)+1;99))
Please note that my solution was using my spreadsheet application OpenOffice. I think Excel has a different find method (Search?) - so make adjustments as necessary.
The above answer is incorrect.
In fact, for my own name, Jay Jacob Wind, the formula breaks.
The correct formula is:
Assuming Last, First is in column A, separated by one comma and a space bar
For first name:
=mid(A2,find(",",A2)+2,99)
For last name:
=mid(A2,1,find(",",A2)-1)
Put together, assuming Column B is First Name and Column C is Last Name
=B2&" "&C2
or simply (but more complexly)
=mid(A2,find(",",A2)+2,99)&" "&mid(A2,1,find(",",A2)-1)
but I like separating them into two columns,
then concatenating them

Resources