This coding verifies if an Email Is Correct, where is the Next without a For, or coding error?

HobbledeHoy

New Member
Joined
Feb 26, 2014
Messages
3
This coding verifies if an Email Is Correct, where is the Next without a For, or coding error?
Code:
Function IsEmailValid(strEmail As String) As Boolean
Dim strArray As Variant
Dim strItem As Variant
Dim i As Long
Dim c As String
blnIsItValid = True
i = Len(strEmail) - Len(Application.Substitute(strEmail, "@", ""))
If i <> 1 Then IsEmailValid = False: Exit Function
ReDim strArray(1 To 2)
strArray(1) = Left(strEmail, InStr(1, strEmail, "@", 1) - 1)
strArray(2) = Application.Substitute(Right(strEmail, Len(strEmail) - _
    Len(strArray(1))), "@", " ")
For Each strItem In strArray
    If Len(strItem) <= 0 Then
    blnIsItValid = False
    IsEmailValid = blnIsItValid
    Exit Function
End If
For i = 1 To Len(strItem)
    c = LCase(Mid(stItem, i, 1))
    If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 _
    And Not IsNumeric(c) Then
    blnIsItValid = False
    IsEmailValid = blnIsItValid
    Exit Function
End If
Next i
If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then
    blnIsItValid = False
    IsEmailValid = blnIsEmailValid
    Exit Function
Next strItem
If InStr(strArray(2), ".") <= 0 Then
     blnIsItValid = False
     IsEmailValid = blnIsItValid
     Exit Function
End If
i = Len(strArray(2)) - InStrRev(strArray(2), ".")
If i <> 2 And i <> 3 And i <> 4 Then
    blnIsItValid = False
    IsEmailValid = blnIsItValid
    Exit Function
End If
If InStr(strEmail, "..") > 0 Then
    blnIsItValid = False
    IsEmailValid = blnIsItValid
    Exit Function
End If
IsEmailValid = blnIsItValid
End Function
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the board.

This section, needs an End If

Rich (BB code):
If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then
    blnIsItValid = False
    IsEmailValid = blnIsEmailValid
    Exit Function
End If
Next strItem
 
Upvote 0
Welcome to the board.

This section, needs an End If

Rich (BB code):
If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then
    blnIsItValid = False
    IsEmailValid = blnIsEmailValid
    Exit Function
End If
Next strItem

Thanks, it worked.

I have a dozen questions.

It is returning false when it should be true. Where is it incorrect in the coding? For True to equal a valid email address.

Please explain in laymen terms what the coding represents in modern day English. Instead of accepting it and just programming from my book, I'd rather understand it in full. So a remarkable post would be nice.

I understand the ins and the outs, but the usage of, for example, Instr, Left, an InstrRev confuses me; after going over it in a vba explained website.

Tips and suggestions will be nice. Expect me here for along time while I master excel. It is a headache, but worth it.
 
Upvote 0
Honestly, I didn't try to figure out the code was doing - I just looked for the syntax error.

What is the email address that are you trying to validate that returns FALSE?
 
Upvote 0
Maybe you modify this function to suit...it also confirms E-Mail Addresses..

Code:
Public Function VM(ByVal strEmailAddress As String) As Boolean
    On Error GoTo Catch
     Dim objRegExp As New RegExp, 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)
    VM = blnIsValidEmail
    Exit Function
 Catch: VM = False
End Function
 
Upvote 0

Forum statistics

Threads
1,215,847
Messages
6,127,270
Members
449,372
Latest member
charlottedv

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