email textbox not working

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
202
Office Version
  1. 2016
Platform
  1. Windows
I'm designing a userform with a textbox (txt_email_1) that the user places an email address. After searching several question forums and web pages I found one. I made some modifications to fit my needs and tried it and it seemed to work UNTIL I placed my personal email address that I've been using over 20 years and the textbox comes back saying 'Invalid Email Address'. First thing I checked was to make sure both 'Microsoft VBScript Regular Expression 5.5' and 'Microsoft Scriptlet Library' are installed and chosen. Now no matter what I place in (good or bad email addresses) I get 'Invalid Email Address'. HELP

(Placed in a module named email)
VBA Code:
Option Explicit



Public Function ValidateEmailAddress(ByVal strEmailAddress As String) As Boolean

On Error GoTo Catch



Dim objRegExp As New RegExp

Dim blnIsValidEmail As Boolean



objRegExp.IgnoreCase = True

objRegExp.Global = True

objRegExp.Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"



blnIsValidEmail = objRegExp.Test(strEmailAddress)

ValidateEmailAddress = blnIsValidEmail



Exit Function



Catch:

ValidateEmailAddress = False

End Function

(Placed in txt_email_1 code)
VBA Code:
Private Sub Txt_email_1_Exit(ByVal cancel As MSForms.ReturnBoolean)

Dim response As Variant

If ValidateEmailAddress(txt_email_1.Text) = False Then

Me.txt_email_1.BackColor = &HC0FFFF 'yellow



response = MsgBox("That Email #1 isn't valid" & Chr(10) _

& "Click OK to re-enter or Cancel if you have no email", vbOKCancel)

If response = vbCancel Then

txt_email_1.Value = ""

Me.txt_email_1.BackColor = &H80000005 'white

Else

If response = vbOK Then

Me.txt_email_1.BackColor = &HC0FFFF 'yellow

Me.txt_email_1.SetFocus

End If

If ValidateEmailAddress(txt_email_1.Text) = True Then

Me.txt_email_1.BackColor = &H80000005 'white

End If



Exit Sub

cancel = True

End If

End If



End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The function worked for me using one address. Did you step through the code and validate that the variables are what you expect, or that lines are executed as you expect?
You don't really need the boolean, but no harm. Could be

ValidateEmailAddress = objRegExp.Test(strEmailAddress)
Exit Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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