Nelson78
Well-known Member
- Joined
- Sep 11, 2017
- Messages
- 526
- Office Version
- 2007
Hello everybody.
I've set up this code to check the correct sintax of e-mail addresses.
Before executing the operation, I bump into an error on the line you can see in the code, because, obviously, something is wrong in transfering the value of ValidEmail (true or false) from the function to the main sub.
How can I figure it out?
Thank's in advance.
I've set up this code to check the correct sintax of e-mail addresses.
Before executing the operation, I bump into an error on the line you can see in the code, because, obviously, something is wrong in transfering the value of ValidEmail (true or false) from the function to the main sub.
How can I figure it out?
Thank's in advance.
VBA Code:
Sub remove_characters_2()
Dim lr As Long
lr = Worksheets(1).Cells(Rows.Count, "D").End(xlUp).Row
For Each cell In Sheets(1).Range("D2:D" & lr)
ValidEmail (cell.Value)
If ValidEmail = False Then cell.Value = "uncorrect address" '*******error here
Next cell
End Sub
Function ValidEmail(ByVal eMail As String) As Boolean
Dim MyRegExp As RegExp
Dim myMatches As MatchCollection
Set MyRegExp = New RegExp
MyRegExp.Pattern = "^[a-z0-9_.-]+@[a-z0-9.-]{2,}\.[a-z]{2,4}$"
MyRegExp.IgnoreCase = True
MyRegExp.Global = False
Set myMatches = MyRegExp.Execute(eMail)
ValidEmail = (myMatches.Count = 1)
Set myMatches = Nothing
Set MyRegExp = Nothing
End Function