Function Returning Wrong Result

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. 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.

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
 
You are making a mistake within thinking, because bReturn only has scope within the procedure of the function you're calling.
This should give you the right result:
VBA Code:
If Target.Address = "$K$12" Then 'email
    sEmailAddress = Target.Value
    MsgBox IsValidEmail(sEmailAddress)
End If
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Thanks GWteB, that was what I needed!
And thank you all for your contributions. There was some good education in your responses that I took away.
 
Upvote 0
You are welcome and thanks for the feedback.
 
Upvote 0
I’d suggest you amend the function to take the pattern as an argument, then call it twice (if necessary) passing each pattern in turn from a wrapper function.
 
Upvote 0
Thanks Rory, that is a good alternative. Is their any particular benefit to doing it in this manner? Always taking opportunity to learn how to avoid pitfalls, cleaner alternatives, and/or improve the efficiency of my code (for instance I still rely far too much on "if/thens", "For/Next" etc)
 
Upvote 0
It will make the function much more reusable since you can pass any pattern and any value to test, not just email addresses
 
Upvote 0
Ahhh ... multipurpose! Great idea. Are there specific/limited patterns for things defined by Microsoft, or is it up to one's imagination? ie, is there a pattern recognized by MS for telephone numbers, or would the user have to create that pattern? I'm not sure if the patterns in my email code are from an internal library, or drawn up by the coder.
 
Upvote 0
Those are regular expressions. You can pretty much create a pattern for anything you can think of once you know the syntax.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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