Module code

keithbc

New Member
Joined
Mar 16, 2009
Messages
26
Hi All

I found this code online to extract email addresses from a string, it works perfectly, except when there are more than 1 email, it will extract them all without a space or comma in between each address. can someone please help me to update the code to add a space or comma between the addresses.
Thanks.
VBA Code:
Function ExtractEmailFun(extractStr As String) As String 'Update 20130829 
Dim CharList As String 
On Error Resume Next 
CheckStr = "[A-Za-z0-9._-]" OutStr = "" Index = 1 Do While True Index1 = VBA.InStr(Index, extractStr, "@") getStr = "" If Index1 > 0 Then For p = Index1 - 1 To 1 Step -1 If Mid(extractStr, p, 1) Like CheckStr Then getStr = Mid(extractStr, p, 1) & getStr Else Exit For End If Next getStr = getStr & "@" For p = Index1 + 1 To Len(extractStr) If Mid(extractStr, p, 1) Like CheckStr Then getStr = getStr & Mid(extractStr, p, 1) Else Exit For End If Next Index = Index1 + 1 If OutStr = "" Then OutStr = getStr Else OutStr = OutStr & Chr(10) & getStr End If Else Exit Do End If Loop ExtractEmailFun = OutStr 
End Function
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
it will extract them all without a space or comma in between each address. can someone please help me to update the code to add a space or comma between the addresses.

Hi, not tested this, but it looks like it separates them with a carriage return, if you want to use a comma instead - try changing this line:

Rich (BB code):
OutStr = OutStr & Chr(10) & getStr


To

Rich (BB code):
Rich (BB code):
Rich (BB code):
OutStr = OutStr & ", " & getStr 
 
Upvote 0
Worked perfectly, thank you.



Hi, not tested this, but it looks like it separates them with a carriage return, if you want to use a comma instead - try changing this line:

Rich (BB code):
OutStr = OutStr & Chr(10) & getStr


To

Rich (BB code):
OutStr = OutStr & ", " & getStr 
 
Upvote 0
Hello FormR, can you please tell me if there is a way to modify this code to only deliver the first result for @? I only want to pull the first email that is in the cell. Thanks.
 
Upvote 0
is a way to modify this code

Hi, that code is garbled for me! It was probably posted using HTML which doesn't get parsed by the new forum software.

In the absence of example data or a written description, here is formula you could try.

Book1
AB
1ds fds fdsfsdf sdf me@internet.com fds fds fds another@world.comme@internet.com
Sheet1
Cell Formulas
RangeFormula
B1B1=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND(" ",A1&" ",FIND("@",A1))-1)," ",REPT(" ",255)),255))
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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