Extracting data before and after a comma

RaviWildcat

Board Regular
Joined
Jun 18, 2010
Messages
124
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello Excel Experts - I have a dilly of a pickle for you

Given the following data:

1. I loaned the sunglasses to Partridge,Keith to use on stage
2. I threw the football to Brady,Marcia but it hit her in the nose
3. Greg bought a guitar and now we call him Bravo,Johnny

I'd like to collect the following information

1. Partridge,Keith
2. Brady,Marcia
3. Bravo,Johnny

I've looked at left() and mid() and find() functions and I can figure out how to get the info if the last names or first names all have the same length. Isn't there a way to get the data to the left and to the right of the comma if I know that they are separated by spaces?

Ravi
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi

Using a helper column

Excel Workbook
ABC
1I loaned the sunglasses to Partridge,Keith to use on stagePartridge,Keith to use on stagePartridge,Keith
2I threw the football to Brady,Marcia but it hit her in the noseBrady,Marcia but it hit her in the noseBrady,Marcia
3Greg bought a guitar and now we call him Bravo,JohnnyBravo,JohnnyBravo,Johnny
Sheet2
Excel 2003
Cell Formulas
RangeFormula
B1=REPLACE(A1,1,FIND(TRIM(RIGHT(SUBSTITUTE(REPLACE(A1,FIND(",",A1),LEN(A1),"")," ",REPT(" ",255)),255)),A1)-1,"")
C1=LEFT(B1,FIND(" ",B1&" ")-1)



Edit: Added a VBA based solution too<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> ExtractName(sInp<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> oMat<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><br>    ExtractName = "No Name Found"<br>    <SPAN style="color:#00007F">With</SPAN> CreateObject("VBScript.RegExp")<br>        .Global =<SPAN style="color:#00007F">True</SPAN><br>        .Pattern = "[A-Za-z]+, ?[A-Za-z]+"<br>        <SPAN style="color:#00007F">If</SPAN> .test(sInp)<SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> oMat = .Execute(sInp)<br>            ExtractName = oMat(0)<br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Function</SPAN></FONT>
Excel Workbook
AB
1I loaned the sunglasses to Partridge,Keith to use on stagePartridge,Keith
2I threw the football to Brady,Marcia but it hit her in the noseBrady,Marcia
3Greg bought a guitar and now we call him Bravo,JohnnyBravo,Johnny
Sheet2
Excel 2003
Cell Formulas
RangeFormula
B1=ExtractName(A1)
 
Last edited:
Upvote 0
Sandeep your vba function is sensational! Thank you for your help!

Ravi
 
Upvote 0
Sandeep your vba function is sensational!
Here is a non-RegExp function that you may want to consider...

Code:
Function ExtractName(S As String) As String
  Dim Parts() As String
  Parts = Split(S, ",")
  On Error Resume Next
  ExtractName = "No Name Found"
  ExtractName = Mid(Parts(0), InStrRev(Parts(0), " ", Len(Parts(0)) - 1) + 1) & "," & Left(Parts(1), InStr(2, Parts(1) & " ", " ") - 1)
End Function
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,488
Members
450,016
Latest member
murarj

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