{"id":2021,"date":"2009-07-07T15:23:05","date_gmt":"2009-07-07T14:23:05","guid":{"rendered":"http:\/\/simoncpage.co.uk\/blog\/?p=2021"},"modified":"2009-07-07T15:23:05","modified_gmt":"2009-07-07T14:23:05","slug":"excel-vb-validate-email-address","status":"publish","type":"post","link":"https:\/\/simoncpage.co.uk\/blog\/2009\/07\/excel-vb-validate-email-address\/","title":{"rendered":"Excel VB | Validate Email Address"},"content":{"rendered":"<p>I generally validate email addresses in Filemaker and find this a much quick and easier solution. But I get asked a fair bit how would you be able to validate an email address in Excel.<\/p>\n<p>There are a couple of ways to do this, a quick and simple approach is to simply check if the cell is a hyperlink (i.e. Excel has done a check as it was entered) and then check for &#8220;@&#8221;.<\/p>\n<blockquote><p>Sub IsValidEMailAddress_Simple()<br \/>\nRange(&#8220;A1&#8221;).Select<br \/>\nIf ActiveCell.Hyperlinks(1).Type = msoHyperlinkRange Then<br \/>\nintChar = InStr(1, ActiveCell.Hyperlinks(1).Address, &#8220;@&#8221;)<br \/>\nMsgBox ActiveCell.Value &amp; &#8221; not hyperlink&#8221;<br \/>\n&#8216;Was the &#8216;@&#8217; found?<br \/>\nIf intChar &gt; 0 Then<br \/>\n&#8216;Does hyperlink contain &#8216;@&#8217;?<br \/>\nMsgBox ActiveCell.Value &amp; &#8221; missing @&#8221;<br \/>\nEnd If<br \/>\nEnd If<br \/>\nEnd Sub<\/p><\/blockquote>\n<p>This generally isn&#8217;t ideal and so for a much more comprehensive check this code (an Excel VB function) will fully validate a referenced cell with an email address in as a true or false (boolean). There is a text file download at the end incase you get formatting issues with copy and paste.<\/p>\n<blockquote><p>Public Function IsValidEMailAddress( _<br \/>\nByVal EMailAddress As String, _<br \/>\nOptional ByVal Strict As Boolean = False _<br \/>\n) As Boolean<\/p>\n<p>&#8216; Return True if the email address referenced is valid, False otherwise.<\/p>\n<p>Const Domain_Extensions = &#8220;|aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|&#8221;<br \/>\nConst Country_Extensions = &#8220;|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|<\/p>\n<p>bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|<\/p>\n<p>eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|<\/p>\n<p>hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|<\/p>\n<p>lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|<\/p>\n<p>na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|<\/p>\n<p>sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|<\/p>\n<p>tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw|&#8221;<br \/>\nConst Invalid_Chars = &#8220;\/&#8217;\\&#8221;&#8221;;:?!()[]{}^| &#8221;<br \/>\nConst Invalid_Chars_Strict = &#8220;\/&#8217;\\&#8221;&#8221;;:?!()[]{}^|$&amp;*+=`&lt;&gt;,% &#8221;<br \/>\nConst Invalid_Domains = &#8220;|aso|dnso|icann|internic|pso|afrinic|apnic|arin|example|gtld-servers|iab|iana|iana-servers|iesg|ietf|irtf|istf|lacnic|latnic|rfc -editor|ripe|root-servers|nic|whois|www|arpa|&#8221;<\/p>\n<p>Dim Index As Long<br \/>\nDim Extension As String<br \/>\nDim Domain As String<br \/>\nDim Position1 As Long<br \/>\nDim Position2 As Long<\/p>\n<p>If Len(EMailAddress) = 0 Then<br \/>\nIsValidEMailAddress = True<br \/>\nExit Function<br \/>\nEnd If<\/p>\n<p>EMailAddress = LCase(EMailAddress)<\/p>\n<p>&#8216; Check for invalid characters<br \/>\nIf Strict Then<br \/>\nFor Index = 1 To Len(EMailAddress)<br \/>\nIf InStr(Invalid_Chars_Strict, Mid(EMailAddress, Index, 1)) &gt; 0 Then<br \/>\nExit Function<br \/>\nEnd If<br \/>\nNext Index<br \/>\nElse<br \/>\nFor Index = 1 To Len(EMailAddress)<br \/>\nIf InStr(Invalid_Chars, Mid(EMailAddress, Index, 1)) &gt; 0 Then<br \/>\nExit Function<br \/>\nEnd If<br \/>\nNext Index<br \/>\nEnd If<\/p>\n<p>&#8216; Check for valid extension<br \/>\nIndex = InStrRev(EMailAddress, &#8220;.&#8221;)<br \/>\nIf Index = 0 Then Exit Function<br \/>\nExtension = Mid(EMailAddress, Index + 1)<br \/>\nIf InStr(Domain_Extensions, &#8220;|&#8221; &amp; Extension &amp; &#8220;|&#8221;) = 0 And InStr(Country_Extensions, &#8220;|&#8221; &amp; Extension &amp; &#8220;|&#8221;) = 0 Then Exit Function<\/p>\n<p>&#8216; Check for consecutive dots<br \/>\nIf InStr(EMailAddress, &#8220;..&#8221;) &gt; 0 Then Exit Function<\/p>\n<p>&#8216; Check for more than one ampersand<br \/>\nIf InStr(Replace(EMailAddress, &#8220;@&#8221;, &#8221; &#8220;, Count:=1), &#8220;@&#8221;) &gt; 0 Then Exit Function<\/p>\n<p>&#8216; Check for text prior to the ampersand<br \/>\nIndex = InStr(EMailAddress, &#8220;@&#8221;)<br \/>\nIf Not Index &gt; 1 Then Exit Function<\/p>\n<p>&#8216; Check for a period after the ampersand<br \/>\nIf Mid(EMailAddress, Index + 1, 1) = &#8220;.&#8221; Then Exit Function<\/p>\n<p>Position1 = InStr(EMailAddress, &#8220;@&#8221;) + 1<br \/>\nPosition2 = InStr(Position1, EMailAddress, &#8220;.&#8221;) &#8211; 1<br \/>\nDomain = Mid(EMailAddress, Position1, Position2 &#8211; Position1 + 1)<\/p>\n<p>If Strict Then<br \/>\n&#8216; Check for single character domain<br \/>\nIf Len(Domain) = 1 Then Exit Function<br \/>\n&#8216; Check for an invalid domain<br \/>\nIf InStr(Invalid_Domains, &#8220;|&#8221; &amp; Domain &amp; &#8220;|&#8221;) &gt; 0 Then Exit Function<br \/>\nIf InStr(Domain_Extensions, &#8220;|&#8221; &amp; Domain &amp; &#8220;|&#8221;) &gt; 0 Then Exit Function<br \/>\nEnd If<\/p>\n<p>&#8216; Check for dash in the first, last, third, or fourth position of the domain<br \/>\nIf Left(Domain, 1) = &#8220;-&#8221; Then Exit Function<br \/>\nIf Right(Domain, 1) = &#8220;-&#8221; Then Exit Function<br \/>\nIf Len(Domain) &gt; 2 Then<br \/>\nIf Mid(Domain, 3, 1) = &#8220;-&#8221; Then Exit Function<br \/>\nIf Len(Domain) &gt; 3 Then<br \/>\nIf Mid(Domain, 4, 1) = &#8220;-&#8221; Then Exit Function<br \/>\nEnd If<br \/>\nEnd If<\/p>\n<p>&#8216; Check for more then 67 characters in the domain and extension<br \/>\nIf Len(Domain) + Len(Extension) &gt; 67 Then Exit Function<\/p>\n<p>IsValidEMailAddress = True<\/p>\n<p>End Function<\/p><\/blockquote>\n<p><a href=\"https:\/\/simoncpage.co.uk\/blog\/wp-content\/uploads\/2009\/07\/isvalidemailaddress.txt\" target=\"_blank\">Download as a text file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I generally validate email addresses in Filemaker and find this a much quick and easier solution. But I get asked a fair bit how would you be able to validate an email address in Excel. There are a couple of ways to do this, a quick and simple approach is to simply check if the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[26],"tags":[123,24,79,78,77,117,655,654,27,119,121],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/2021"}],"collection":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=2021"}],"version-history":[{"count":0,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/2021\/revisions"}],"wp:attachment":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}