Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,650
- Office Version
- 365
- 2016
- Platform
- Windows
I found this webpage providing me with a Function to check email address validity. (Validate Email Address - VBA Code - Excel)
I made some adjustments in order to check the two different available patterns, but even when I put in a valid email address (xaxaxaxaxa@gmail.com), the result is always false. I'm wondering if someone could advise what part of my code is contributing to the wrong answer.
I made some adjustments in order to check the two different available patterns, but even when I put in a valid email address (xaxaxaxaxa@gmail.com), the result is always false. I'm wondering if someone could advise what part of my code is contributing to the wrong answer.
Code:
Function IsValidEmail(sEmailAddress As String) As Boolean
'Code from Officetricks (https://officetricks.com/validate-email-address-vba-code-excel/)
'Define variables
'Stop
Dim sEmailPattern As String
Dim oRegEx As Object
Dim bReturn As Boolean
'Use the below regular expressions
sEmailPattern1 = "^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" 'or
sEmailPattern2 = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
'Create Regular Expression Object
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Global = True
oRegEx.IgnoreCase = True
oRegEx.Pattern = sEmailPattern
bReturn = False
'Check if Email match regex pattern
If oRegEx.Test(sEmailAddress1) Or oRegEx.Test(sEmailAddress2) Then
'Debug.Print "Valid Email ('" & sEmailAddress & "')"
bReturn = True
Else
'Debug.Print "Invalid Email('" & sEmailAddress & "')"
bReturn = False
End If
'Return validation result
IsValidEmail = bReturn
End Function