VBA to remove all email addresses from cells

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
246
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I was using a find & replace @*, to remove all email addresses. It was working fine until a new person joined and starting adding @ in front of every person's name thinking it will be like in Outlook (grrrrr). So when I do the find & replace, it removes everything in the cell instead of only removing the email addresses.
I am open to suggestions on how to remove all the email addresses only from one column. The other content before or after an email address must not be deleted however, unless it is another email address. I can not delete by domain because there is just thousands of variations.

Here is an example of what it a typical entry looks like in each cell under column "F":
June 27, 2024
Meeting with Jack Black - jack.black@sample.com
June 26, 2024
Unable to verify funds from deadbeat@home.pa
June 25, 2024
Package sent to momma@home.us

Wondering if I can revise this script to suit my needs somehow. It checks the string for @. If the @ is present, it looks for the preceding comma and clips the string at the point
VBA Code:
Public Function noemail(s As String) As String
    Dim i As Long, L As Long

    If InStr(s, "@") = 0 Then
        noemail = s
        Exit Function
    End If
  
    L = Len(s)
    For i = L To 1 Step -1
        If Mid(s, i, 1) = "," Then
            noemail = Left(s, i - 1)
            Exit Function
        End If
    Next i
  
End Function
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
VBA Code:
Public Function noemail(s As String) As String
   
    Dim email As Object
   
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "([a-zA-ZF0-9\u00C0-\u017F._-]+@[a-zA-Z0-9\u00C0-\u017F._-]+\.[a-zA-Z0-9\u00C0-\u017F_-]+)|([a-zA-Z0-9\u00C0-\u017F._-]+\.[a-zA-Z0-9\u00C0-\u017F_-]+)"
        For Each email In .Execute(s)
            s = Replace(s, email, "")
        Next
    End With
   
    noemail = Application.Trim(s)
 
End Function
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,652
Latest member
eduedu

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