VBA: Extract matching first name from email address

gmcelroy

New Member
Joined
Feb 26, 2015
Messages
10
Hi All,

I have a long list of email addresses, all in random formats (ie. not your standard first.last@domain.com).

I also have a long list of 5k first names.

The macro needs to search each email address for a matching first name from 5k list and return the name when a match occurs. The logic is pretty straightforward:

1. Return a value if the email address STARTS with one of the names in the list of 5k names. For example:
The email "chris@gmail.com" should return "Chris" but "merrychristmas@gmail.com" shouldn't return anything.​

2. In cases where more than one match exists, return the longest match. For example:

The email "danielle@gmail.com" would match with "dan", "daniel", and "danielle". In these cases, "Danielle" should be returned since it is the longest match.​

3. If there is no match, the macro shouldn't return anything.

I tried accomplishing this using a formula and got pretty far, but can't get it to work fully. Thought I would ask the VBA experts out there if they can come up with a solution.

Any help is greatly appreciated!!

Thanks,
Glenn
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
bobbob@gmail.com@@@@@
fredfred@gmail.com
sue#N/A
prudence#N/A
fredericfrederic@gmail.com
#####
bob@gmail.combob
sid@gmail.comsid
tom@gmail.comtom
fred@gmail.comfred
wayne@gmail.comwayne
fatherxmas@gmail.comfatherxmas
cristopher@gmail.comcristopher
frederic@gmail.comfrederic
#####
=LEFT(H14,SEARCH("@",H14)-1)
@@@@@
=OFFSET($I$13,MATCH(A2,$I$14:$I$21,0),-1)

<colgroup><col><col><col span="5"><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Hi Oldbrewer,

Thanks for your reply, but I'm not quite sure I understand your solution. Hopefully the example below further explains what I'm looking for:

Column A = List of email addresses
Column B = Extracted first name (if there is a match)
Column C = List of all 5k first names



ABC
glenn123@gmail.comGlennAlex
allstarcook@yahoo.comChris
steve.jones@hotmail.comSteveDan
merrychristmas@gmail.comDaniel
chris.walkin@yahoo.comChrisDanielle
daniel5593@msn.comDanGlenn
harry_alex@gmail.comMark
Steve
...

<tbody>
</tbody>


I believe your solution is just returning the text left of the "@" symbol which won't work since the emails all have different formatting.
 
Last edited:
Upvote 0
agreed, but you then look for a match with any of your list of first names - harry_alex will match with harry and alex
 
Upvote 0
agreed, but you then look for a match with any of your list of first names - harry_alex will match with harry and alex

That's one of the problems I was running into at first. For that reason, the first criteria in my OP is as follows:

1. Return a value if the email address STARTS with one of the names in the list of 5k names. For example:
The email "chris@gmail.com" should return "Chris" but "merrychristmas@gmail.com" shouldn't return anything.

In the case of harry_alex@gmail.com, only "Harry" should be returned.

Do you think this can be done using a formula? Or is VBA needed?

Thanks for your help oldbrewer!
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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