20

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?

Matteo
  • 14,696
  • 9
  • 68
  • 106
Hassan Ata Ullah
  • 347
  • 1
  • 2
  • 16
  • ... works fine when I tested it... I had to change the `“` characters to `"`, though... – John Bustos Jan 23 '13 at 15:08
  • Are you really using “” and not ""? – Matteo Jan 23 '13 at 15:11
  • On a side note: a space is allowed: see http://en.wikipedia.org/wiki/Email_address#Local_part – Matteo Jan 23 '13 at 15:12
  • I re-edited the question with correct quotes (") – Matteo Jan 23 '13 at 15:23
  • 2
    @Matteo by editing you RUINED the meaning of the question...((( – Peter L. Jan 23 '13 at 15:26
  • @PeterL. Of course not: the problem was not the quotes but the fact that the formula is returning either true or #VALUE! – Matteo Jan 23 '13 at 15:28
  • You say you are validating the addresses - how exactly? Are you using the formula in a cell on the worksheet or within Data Validation? – barry houdini Jan 23 '13 at 15:29
  • @Matteo "but excel comes up with error that the formula you typed contains an error." - that's NOT a value in cell, but Excel message box - try to type original formula or simply use "not plain" quote - you'll get the same. Extra / missing bracket will come up to this as well. – Peter L. Jan 23 '13 at 15:31
  • @PeterL. Yes I see now ... Reverted the edit – Matteo Jan 23 '13 at 15:35

5 Answers5

24

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)

Peter L.
  • 7,276
  • 5
  • 34
  • 53
  • To be precise, your double quotes have codes 147 and 148 (use `CODE` function to check), while "plain" is char with code 34. – Peter L. Jan 23 '13 at 15:12
  • Thanks for the answer but I got the same error when i copied the formula u provided. – Hassan Ata Ullah Jan 23 '13 at 15:14
  • @HassanAtaUllah Seems strange - check my sample file: https://www.dropbox.com/s/gavgg48733m3918/MailCheck.xlsx – Peter L. Jan 23 '13 at 15:18
  • Does not work as FIND will return an error if nothing if found – Matteo Jan 23 '13 at 15:22
  • @Matteo that's why formula has `ISERROR` wrapper! check my sample file in the above comment - it works fine. – Peter L. Jan 23 '13 at 15:24
  • Are you using in "Data Validation"? If so then you may get a mesage saying "the formula currently evaluates to an error" but that doesn't mean you can't use it.... – barry houdini Jan 23 '13 at 15:24
  • @PeterL. If there is no "@" the first `FIND` will return `#VALUE!` you have to wrap it too – Matteo Jan 23 '13 at 15:25
  • @HassanAtaUllah "In your answer you use "," instead of ";"" - that's because of regional settings: my region uses ";" as separator in arguments, while other regions do use comma. – Peter L. Jan 23 '13 at 15:29
  • @PeterL. Take the example you put on Dropbox and try to enter an invalid address like "aaa". You will see the problem – Matteo Jan 23 '13 at 15:30
  • ah then the problem is solved thx alot for the help peter L :) – Hassan Ata Ullah Jan 23 '13 at 15:30
  • @PeterL. I didn't notice that formula was used for validation and that #VALUE is OK. I reverted the edit on the question – Matteo Jan 23 '13 at 15:32
  • @Matteo updated sample file and added more correct version to my post. Thanks, that was useful! – Peter L. Jan 23 '13 at 15:44
  • 2
    Just in case another German wants to use the formula - here is the German version: =UND(NICHT(ISTFEHLER(FINDEN("@"; A2))); NICHT(ISTFEHLER(FINDEN("."; A2))); ISTFEHLER(FINDEN(" "; A2))) – Olaf Aug 06 '13 at 09:09
  • Turkish version is below. replace comma to semicolon(;) =AND(NOT(ISERROR(FIND("@";A1)));NOT(ISERROR(FIND(".";A1)));ISERROR(FIND(" ";A1))) – Sedat Kumcu Apr 08 '15 at 14:16
  • a.a@a is valid with this answer. Another possible solution =AND(IFERROR(FIND(".",A2),FALSE),IFERROR(FIND(".",A2,FIND("@",A2)),FALSE)) – dariogriffo Jan 25 '17 at 11:26
12

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

Tiny
  • 27,221
  • 105
  • 339
  • 599
Joel
  • 467
  • 1
  • 6
  • 18
4

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).

user3819867
  • 1,114
  • 1
  • 8
  • 18
  • This is a fair answer, and seems to work for basic emails. but what about more advanced emails? I get a false negative for emails with formats like: bob.jones@foobar.com.au OR bob.jones@foobar.com – Djinné22 Sep 22 '15 at 22:44
1

=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

dariogriffo
  • 4,148
  • 3
  • 17
  • 34
0

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
Le Droid
  • 4,534
  • 3
  • 37
  • 32