Vba: how to transfer the boolean value of a function to the main sub

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 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.

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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Replace this:

VBA Code:
    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

with this:

VBA Code:
    For Each cell In Sheets(1).Range("D2:D" & lr)
               If ValidEmail(cell.Value) = False Then cell.Value = "incorrect address" '*******error here
    Next cell
 
Upvote 0
Solution

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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