Reputation: 357
I have a column where people enter email address manually. I want to validate the email address using this formula:
=AND(FIND(“@”,A2),FIND(“.”,A2),ISERROR(FIND(” “,A2)))
but excel comes up with error that the formula you typed contains an error. For me the formula looks right. Do you guys have any suggestions?
Upvotes: 20
Views: 109325
Reputation: 3064
For anyone looking for a quick/easy solution in 2024...
I'm not sure if the SEARCH
function was available when the question was first posed, but it's a big help now:
=IFERROR(SEARCH("*?@?*.??*",$A2)=1,FALSE)
Probably not as thorough as the VBA options as it doesn't catch any bad input AFTER a genuine email address (e.g. [email protected].@), but it's good enough for my requirements.
Upvotes: 0
Reputation: 4774
Inspired from Joel's solution but shorter. Does the same checks:
Function IsEmailValid(strEmail)
Dim i As Integer, emailPart As Variant
IsEmailValid = IsMadeOf(LCase(strEmail), "abcdefghijklmnopqrstuvwxyz0123456789.-_@")
emailPart = Split(strEmail, ".")
i = 0
While IsEmailValid And i <= UBound(emailPart)
IsEmailValid = Len(emailPart(i)) > IIf(i = UBound(emailPart), 1, 0)
i = i + 1
Wend
If IsEmailValid Then
emailPart = Split(strEmail, "@")
IsEmailValid = UBound(emailPart) = 1 And InStr(emailPart(UBound(emailPart)), ".") > 0
End If
End Function
Function IsMadeOf(str, charList)
Dim i As Long, c As String
IsMadeOf = True
For i = 1 To Len(str)
c = Mid(str, i, 1)
If InStr(charList, c) <= 0 Then
IsMadeOf = False
Exit Function
End If
Next i
End Function
Upvotes: 0
Reputation: 4274
=AND(IFERROR(FIND(".",A2),FALSE),IFERROR(FIND(".",A2,FIND("@",A2)),FALSE))
This will validate the . is after the @ which is not tested on the accepted answer
Upvotes: 1
Reputation: 1120
I bumped into an issue of firstname.lastname@domain@topdomain
for which I made an amendment that checks the correct order of the @
and the .
with an implicit Like
without VBA.
=AND(NOT(ISERROR(VLOOKUP("*@*.*",A2,1,FALSE))),ISERROR(FIND(" ",A2)))
EDIT
"*?@?*.??*"
seems to be even more descriptive as long as top-level domains are at least two characters long (as of this post they are).
Upvotes: 4
Reputation: 477
Another way to validate emails in excel is using VBA code: see code below taken from http://www.vbaexpress.com/kb/getarticle.php?kb_id=281, it works great as is, and you can modify the code based on your needs.
Sub email()
Dim txtEmail As String
txtEmail = InputBox("Type the address", "e-mail address")
Dim Situacao As String
' Check e-mail syntax
If IsEmailValid(txtEmail) Then
Situacao = "Valid e-mail syntax!"
Else
Situacao = "Invalid e-mail syntax!"
End If
' Shows the result
MsgBox Situacao
End Sub
Function IsEmailValid(strEmail)
Dim strArray As Variant
Dim strItem As Variant
Dim i As Long, c As String, blnIsItValid As Boolean
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(strItem, 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 = blnIsItValid
Exit Function
End If
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 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
For how to instructions check http://www.vbaexpress.com/kb/getarticle.php?kb_id=281#instr
Upvotes: 12
Reputation: 7304
I got the same error for your code, and it appears that you have NOT "plain" double quotes, that is different from this symbol: "
.
Try my spelling: =AND(FIND("@",A2),FIND(".",A2),ISERROR(FIND(" ",A2)))
- hope will help!
EDIT:
In addition, consider to use =AND(NOT(ISERROR(FIND("@",A1))),NOT(ISERROR(FIND(".",A1))),ISERROR(FIND(" ",A1)))
- that will prevent errors in case @
or .
are missing. Still, this will pass as OK aaa@.
, but I suppose even such straightforward approach has rights to be used)
Upvotes: 24