Wednesday, November 18, 2009

Excel Macro : Email Address Extract from String

Last week I got a mail from my friend asking for help in extracting email IDs from data available with him. The position of the mail IDs within the text string was not same and he was finding it difficult to us extract email addresses.

I wrote a quick UDF for him which did the job. I am sharing same with you as you may find it use full.

Sample of the data and mail IDs extracted with the UDF is as below



Here is the VBA code to Email Address Extract from String. You will need to copy this code to regular VBA module of your workbook

Function Getmailid(cell As Range) As String

Dim Textstrng As String

Textstrng = cell.Text
Position@ = InStr(1, Textstrng, "@")
EmStart = InStrRev(Textstrng, " ", Position@)
If EmStart = 0 Then EmStart = 1
EmEnd = InStr(Position@, Textstrng, " ")
If EmEnd = 0 Then EmEnd = Len(Textstrng) + 1

mailid = Trim(Mid(Textstrng, EmStart, EmEnd - EmStart))

If Right(mailid, 1) = "." Then
Getmailid = Left(mailid, Len(mailid) - 1)
Else
Getmailid = mailid
End If
End Function

Download file with VBA code to Email Address Extract from Text String

59 comments:

  1. Did you change your code displayer? I don't remember this happening before, but when I copy/pasted your code into my copy of XL2003, everything was on a single line (that is, there were no line feeds between the individual statements).

    ReplyDelete
    Replies
    1. Great Article Cloud Computing Projects

      Networking Projects

      Final Year Projects for CSE

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Here is a worksheet formula that will extract an email address from the text in a cell...

    =TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("@",A1)-1)," ",REPT(" ",99)),99))&MID(A1,FIND("@",A1),FIND(" ",A1&" ",FIND("@",A1))-FIND("@",A1))

    In case your code displayer ends up breaking the line at blank spaces, I just want to point out that there are three blank spaces surrounded by quote marks in the formula... one on each side of the REPT function and one after the third FIND function.

    Rick Rothstein (MVP - Excel)

    ReplyDelete
  3. I just noticed that my formula has **four** blank spaces in it, not three and it looks like your display word wrapped it at the space I forgot to count. However, that should not matter since when I copy the formula from your webpage, it pastes correctly into Excel's Formula Bar as a single continuous line.

    ReplyDelete
  4. Hello Rick

    I have not made any change in the code display. Could not understand why is it behaving like this. You can download the excel file having the code.


    Thanks for your formula input.

    Regards//Yogesh Gupta

    ReplyDelete
  5. Here is a macro that is a little more robust than the one you posted... instead of using just a blank space to delimit the email address from the rest of the text, it uses any character that is invalid in an email address to delimit the email address from the rest of the text. That way, you will be able to retrieve the email address if it is, for example, surrounded by parentheses, quote marks, etc. You will also be able to find the email address if it located at the end of a sentence (email addresses cannot start or end with a dot). Here is the code...

    Function GetEmailAddress(ByVal S As String) As String
    Dim X As Long, AtSign As Long
    Dim Locale As String, DomainPart As String
    Locale = "[A-Za-z0-9.!#$%&'*/=?^_`{|}~+-]"
    Domain = "[A-Za-z0-9._-]"
    AtSign = InStr(S, "@")
    For X = AtSign To 1 Step -1
    If Not Mid(" " & S, X, 1) Like Locale Then
    S = Mid(S, X)
    If Left(S, 1) = "." Then S = Mid(S, 2)
    Exit For
    End If
    Next
    For X = AtSign + 1 To Len(S) + 1
    If Not Mid(S & " ", X, 1) Like Domain Then
    S = Left(S, X - 1)
    If Right(S, 1) = "." Then S = Left(S, Len(S) - 1)
    GetEmailAddress = S
    Exit For
    End If
    Next
    End Function

    Also note that I changed the argument's data type from Range to String... doing this still allows you to pass a cell reference into it when using it as a UDF, but it also allows you to pass it a simple text string as well (useful if you want to use the function in a non-UDF setting).

    Rick Rothstein (MVP - Excel)

    ReplyDelete
  6. Hi Rick

    Thanks for your inputs. I tested the code and feel that there is some gap. After the first for next blok, "AtSign" variable needs to be recalculated by using statement
    AtSign = InStr(S, "@")

    It works fine after this change.

    I have updated the file at download link in the original post to include your code after this correction.

    Regards//Yogesh Gupta

    ReplyDelete
  7. Good catch! That is what I get for making last minute changes and not testing them. I'm glad spotted the problem and were able to figure out the solution. Thanks.

    Just a thought... as long as you decided to include my code in the download file, you might want to include a comment as to what the difference is between your code and mine, just in case someone reads your blog entry and downloads the file directly without reading the comments.

    Rick Rothstein (MVP - Excel)

    ReplyDelete
  8. Though I had included credits to you in the download file but not mentined about the differnece between two.

    Now I have mentioned the difference and updated the file at download link.

    Now some thing beyond this post. I want to invite you for guest post on my blog. Any topic or any tips of your choice. Let me know if you are okay with this. I will be honored if you accept my invite.

    ReplyDelete
  9. Hi Yogesh,

    Your blog is awsome to excel learners.
    I have 300+ pdf files which i have to load into one excel sheet about its contents can u show me a formula or a vb code to create one excel file having details of all pdf files..

    Is there any way to do that
    Hope you understand its urgency!!!

    ReplyDelete
  10. Hi Niku

    I am not clear about your question.

    If you want to read and update the content of the PDF files into Excel Sheet. Then I do not have solution for you.

    If you just want the list of the files to be updated then it can be done with the help of VBA.

    Pls revert back.

    Regards

    ReplyDelete
  11. Thanks for your reply

    I m clearing my question I have a file in which details of customers available eg.cust name mobile number etc.. one file for each customer and i have to make MIS of that pdf files we have generated online in one excel file.
    so any solution for that looking for early reply

    ReplyDelete
  12. Hi Niku

    I do not have solution for this.

    Regards

    ReplyDelete
  13. I have a list of company websites that i would like to extract the email addresses. Anyone would be able to explain how this is done in Excel? Or of this is even possible?

    ReplyDelete
  14. Hi,

    I have one excel sheet in shared folder and whenever that sheet is updated by any employee i should receive mail with the updated string. could u be please help me for the solution.

    Thanks,
    Jayesh

    ReplyDelete
  15. Hi Sir,
    The macro for finding emails from string helped us a lot. But we are able to find first occurence of emails alone. Can you help me in finding multiple emails in string?

    ReplyDelete
  16. I cannot get the VB to work. Could you kindly explain exactly how to open and run a VB? Ever so sorry. Actually, what i am trying to do overall is:

    1. In Excel extract 2000 email addresses from a single spreadsheet (parents who have subscribed by filling and signing a form) ;
    2. In Outlook, send a message to these addresses at the rate 100 per hour ;
    3. In Excel, remove the bounced addresses from the spreadsheet ;
    4. Do the same process from time to time from the same (but probably modified) spreadsheet.

    thanks in advance

    ReplyDelete
  17. For data where some strings may contain more than one email address, is there a way to alter Rick's function so that ALL address within the string are extracted?

    ReplyDelete
  18. HI

    can you tell me know what is the code required or any thing i can do if we need to extract many email id’s that are inside a single cell.

    ReplyDelete
  19. I am new to this. I followed the instructions but now how do I run it to pull emails form the Excel file?

    ReplyDelete
  20. Here is another Free Excel tool that extract emails from a webpage or text. Also has Source code.
    http://officetricks.com/email-extractor-harvesting-email-addresses/

    ReplyDelete
  21. Hi, I am wanting to include a button on my Excel that when activated automatically emails certain fields to an individual.

    ReplyDelete
  22. Hi Yogesh,

    I use the given vba coding and put in new work but its not working its asking new micro name.. would you like to suggest me what have to do first to get the result : chaudhary.rakesh23@gmail.com

    ReplyDelete
  23. please notify me

    ReplyDelete
  24. Email Extractor Files has been purposely designed to harvest email addresses from files of different formats. This amazing software has been given all the latest & unique characteristics that make the task of extracting email ids easy & effortless.
    extract emails

    ReplyDelete
  25. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. find contact details

    ReplyDelete
  26. Many years later: thank you for this, still works perfectly!

    ReplyDelete
  27. PDF records are broadly utilized now-a-days by numerous individuals. Nonetheless, a PDF has both positive and negative angles. Since, it is a non-editable arrangement, you can't roll out any improvements in the PDF document. In this way, you have to change over these records to word. https://www.altoextractpdf.com/

    ReplyDelete
  28. Email List USA This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs…

    ReplyDelete

  29. It is so useful blog, Thanks for sharing this information. I appreciate this blog's information.
    Please keep sharing this type of blog. web email extractor

    ReplyDelete
  30. So, if there any new product launch or service, you can share it on your Facebook page and add your website link so your people can easily check your website. So, you should use the Facebook page for promotion & also build an email list.Email List for Marketing

    ReplyDelete

  31. So nice and informative Blog. Thanks for posting this Blog. We hope to keep sharing this type of information.

    Email Extractor Files

    ReplyDelete
  32. You understand your projects stand out of the crowd. There is something unique about them. It seems to me all of them are brilliant. sign in to Hotmail

    ReplyDelete
  33. Your website is really cool and this is a great inspiring article. ActiveCampaign Enterprise plan

    ReplyDelete
  34. Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards,
    find email

    ReplyDelete
  35. I just want to let you know that I just check out your site and I find it very interesting and informative.. email search engine

    ReplyDelete
  36. I am always searching online for articles that can help me. There is obviously a lot to know about this. I get to you a new game. This is hot games in the year. Click link to visit site. sims 4 cheats

    ReplyDelete
  37. That's really awesome, Thankyou for sharing this inforamtion with us. Keep us updated. You friend really resolved my problem
    hotmail

    ReplyDelete
  38. I have seen some great stuff here. Worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Your work is truly appreciated around the clock and the globe. click here

    ReplyDelete
  39. thank you for this such a good information about hotmail email login on www.hotmail.com

    ReplyDelete
  40. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. business email

    ReplyDelete
  41. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. email search engine

    ReplyDelete
  42. PHP is a language which is very straightforward and is perfect with a few databases, and is picked by numerous engineers and clients. Cakephp Programming service

    ReplyDelete
  43. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. iniciar una sesion en hotmail

    ReplyDelete
  44. Really wanted to thankyou for this post.. works really fine for me.... Really solved my problem using your post.. keep sharing information like this.. hotmail login

    ReplyDelete
  45. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work.
    hotmail

    ReplyDelete
  46. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.

    google photos login

    ReplyDelete
  47. Hotmail.com Login: how to login to https://hotmail-loginaccount.info/
    , Outlook, MSN, Live, Email... on the web, desktop, mobile & apps. Sign up for a Hotmail Account and sign in!

    ReplyDelete
  48. Indeed a very informative post I was having trouble with the excel earlier but yahoo mail solved it. Thanks alot for the great share elder scrolls 6

    ReplyDelete
  49. Our website is popular all around the globe especially in UAE, Saudi Arabia, Middle East, Dubai, Abu Dhabi, Oman, Qatar, and many other countries.Keep visiting our website. We will update all the Pinoy Tv, GMA Network show and ABS-CBN network shows daily. This website is a golden platform for the overseas Filipinos living all-around the world.

    ReplyDelete
  50. Pangako Sa'Yo is dubbed as the country's first-ever teleserye Pinoy Tv Replay and was considered one of the most significant turning points in Philippine television history due to its pioneering cinematic production and modified fast-paced, multiple-arc story structure distinguishing it from previous Filipino telenovelas.

    ReplyDelete
  51. Here are the top 8 Pinoy Flix that are loved the most around the world.

    ReplyDelete
  52. Woderful content. I am regularly follow this blog. Thank you for updating such a good content. Please follow my Travel blog for more information about Eco Park in Kolkata.

    My other posts are:
    Victoria Memorial in Kolkata.
    Scuba Diving in Goa.
    Top visiting places in GOA.
    Calangute Beach in Goa.
    Candolim Beach in Goa.
    Baga Beach in Goa.

    ReplyDelete
  53. Deloitte vs osl We are an expert iPhone app development company in Canada creating customized and responsive mobile apps for the best user experience

    ReplyDelete
  54. Watch and Download world's famous Turkish action drama Kurulus Osman Season 3 in English on link below
    👇
    Kurulus Osman Season 3

    Kurulus Osman Season 3 Episode 1
    On link below
    Kurulus Osman Season 3 Episode 1

    Crypto trading course
    Join on link below
    Crypto quantum leap

    YouTube course
    Be a professional YouTuber and start your carrier
    Tube Mastery and Monetization by matt

    Best product for tooth pain ,
    Cavity ,
    Tooth decay ,
    And other oral issues
    Need of every home
    With discount
    And digistore money back guarantee
    Steel Bite Pro

    ReplyDelete
  55. You really do a great job of demonstrating how important today's email function is. When I create assignments using the dissertation on business management service, I use it the majority of the time. I receive assignments via email. However, because it is cumbersome and takes a while to process, I hardly ever use this service.

    ReplyDelete

Learn how to create Excel dashboards.
Yogesh Gupta's Excel Tips