Valid email function - needs a tweak

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
308
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am using the following function to parse a list of email addresses to check if they're valid (if this code was written by someone on here, many thanks, it works a treat!)
VBA Code:
Public Function ValidateEmail(ByVal sEmail As Variant) As Boolean
    On Error GoTo Error_Handler
    Dim oRegEx As Object
 
    If Not IsNull(sEmail) Then
        Set oRegEx = CreateObject("vbscript.regexp")
        oRegEx.Pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
        ValidateEmail = oRegEx.Test(sEmail)
    Else
        ValidateEmail = True
    End If
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oRegEx Is Nothing Then Set oRegEx = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: ValidateEmail" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

I need it amended slightly to accommodate where more than one email address exists in a cell. For example, a cell might contain
Code:
donald.trump@us.gov; vladimir.putin@russia.ru

This would be a valid "address" in the .To field when using VBA to send emails.

Any pointers (or workarounds) appreciated as always.

Thank you for taking the time to read :-)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,225,690
Messages
6,186,463
Members
453,356
Latest member
Zulkifal Shahzad

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