How to Extract Email Address from Text using excel VBA?
Tags: toextractemailaddres, vbamacros
Published: 2021-09-27 07:52:35

---
title: How to Extract Email Address from Text using excel VBA?
published: true
date: 2021-09-20 09:04:06 UTC
tags: ToExtractEmailAddres,VBAMacros
canonical_url: https://geekexcel.com/how-to-extract-email-address-from-text-using-excel-vba/
---

In this tutorial, we will guide you on how to **extract email addresses from text** using Excel Office VBA. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, you need to  **create a sample data**  with more **information in text format**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-25.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Function ExtractEmailFromText(s As String) As String Dim AtTheRateSignSymbol As Long Dim i As Long
    Dim TempStr As String
    Const CharList As String = "[A-Za-z0-9._-]"
    AtTheRateSignSymbol = InStr(s, "@")
    If AtTheRateSignSymbol = 0 Then
        ExtractEmailFromText = ""
    Else
        TempStr = ""
        For i = AtTheRateSignSymbol - 1 To 1 Step -1
            If Mid(s, i, 1) Like CharList Then
                TempStr = Mid(s, i, 1) & TempStr
            Else
                Exit For
            End If
        Next i
        If TempStr = "" Then Exit Function
        TempStr = TempStr & "@"
        For i = AtTheRateSignSymbol + 1 To Len(s)
            If Mid(s, i, 1) Like CharList Then
                TempStr = TempStr & Mid(s, i, 1)
            Else
                Exit For
            End If
        Next i
    End If
    If Right(TempStr, 1) = "." Then TempStr = _
       Left(TempStr, Len(TempStr) - 1)
    ExtractEmailFromText = TempStr
End Function

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-57-1024x581.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, to get the  **output in cell B2** , you have to  **use formula**  given below.

```
=ExtractEmailFromText(A2)
```

![](https://geekexcel.com/wp-content/uploads/2021/09/Use-Formula-1024x338.png)<figcaption>Use Formula</figcaption>

- Finally, you will receive the  **output ** in the  **Microsoft Excel**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output-25.png)<figcaption>Output</figcaption>

## End of the Line

We hope that this tutorial gives you guidelines on how to **extract email addresses from text** using Excel Office VBA. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Further Reference:**

- **[Excel Formulas to Remove File Extension from Filename ~ Simple Tricks!!](https://geekexcel.com/excel-formulas-to-remove-file-extension-from-filename/)**
- **[How to Rename Multiple Files using Macros (VBA) in Excel 365?](https://geekexcel.com/how-to-rename-multiple-files-using-macros-vba-in-excel-365/)**
- **[How to List All Files and Sub-Directories of a Folder in Excel 365?](https://geekexcel.com/how-to-list-all-files-and-sub-directories-of-a-folder-in-excel-365/)**
- **[Split/Save Each Worksheet of One Workbook as Separate Excel/Txt/Csv/Pdf Files in Excel 365!!](https://geekexcel.com/split-save-each-worksheet-of-one-workbook-as-separate-excel-txt-csv-pdf-files-in-excel-365/)**
- **[How to Print Multiple Workbooks/Worksheets from Directories in Excel 365?](https://geekexcel.com/how-to-print-multiple-workbooks-worksheets-from-directories-in-excel-365/)**