+ Reply to Thread
Results 1 to 8 of 8

A Simple Email Validation Question

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2010
    Location
    Lubbock, Texas
    MS-Off Ver
    Excel 2003
    Posts
    4

    A Simple Email Validation Question

    I have a column full of email addresses and I have been trying to create a macro that simply makes sure each address at least has an @ and . in it to eliminate the number of emails that bounce back.

    I would prefer the macro to either check the range I highlighted or even the specific column since it will always be column A.

    Also it would be ideal if it simply just highlighted the invalid emails. I was hoping this would be an easy fix for macro gurus.

    After getting on the Internet and trying to learn VB and macros, below is what I have but its not working and it is using a message box instead of highlighting since I am not sure how to do that.
    Sub ValidEmail()
    Dim Cel As Range
    For Each Cel In Intersect(Selection, ActiveSheet.UsedRange)
    
      If InStr(1, Cel.Value, ".") = 0 Or InStr(1, Cel.Value, "@") = 0 Then
               MsgBox "Email address not valid"
               bNotValid = True
            End If
            Cel.Value = Cel.Value & ";"
    Next
    End Sub
    Thanks for any help and tips.
    Last edited by cfaust; 12-29-2010 at 06:06 PM.

  2. #2
    Registered User
    Join Date
    12-29-2010
    Location
    Lubbock, Texas
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: A Simple Email Validation Question

    Sorry about that.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: A Simple Email Validation Question

    Hello cfaust,

    Welcome to the Forum!

    This macro will highlight any invalid email addresses in column "A" start with cell "A2". I am assuming you have a header in in "A1".
    Sub ValidEmail()
    
      Dim Cel As Range
      Dim Rng As Range
      Dim RngEnd As Range
    
        Set Rng = Range("A2")
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Range(Rng, RngEnd)
    
        For Each Cel In Rng
    
          If Not (Cel Like Like "[!.]*?[!.]@[!-]?*?.?*[!-]") Then
             Cel.Interior.ColorIndex = 6
          End If
     
        Next Cel
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    12-29-2010
    Location
    Lubbock, Texas
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: A Simple Email Validation Question

    Quote Originally Posted by Leith Ross View Post
    Hello cfaust,

    Welcome to the Forum!

    This macro will highlight any invalid email addresses in column "A" start with cell "A2". I am assuming you have a header in in "A1".
    Sub ValidEmail()
    
      Dim Cel As Range
      Dim Rng As Range
      Dim RngEnd As Range
    
        Set Rng = Range("A2")
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Range(Rng, RngEnd)
    
        For Each Cel In Rng
    
          If Not (Cel Like Like "[!.]*?[!.]@[!-]?*?.?*[!-]") Then
             Cel.Interior.ColorIndex = 6
          End If
     
        Next Cel
    
    End Sub
    Thanks a lot for the help and the above code you are right about having a header so starting at A2 is perfect.

    When I run the macro I get a syntax error on the line.
    If Not (Cel Like Like "[!.]*?[!.]@[!-]?*?.?*[!-]") Then

    I am trying to figure out why but I thought I would post it while I attempt to debug.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: A Simple Email Validation Question

    Hello cfaust,

    If only I could type. Remove one of the "Like" statements in the line.
        If Not (Cel Like "[!.]*?[!.]@[!-]?*?.?*[!-]") Then

  6. #6
    Registered User
    Join Date
    12-29-2010
    Location
    Lubbock, Texas
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: A Simple Email Validation Question

    That was it thanks a lot for your help it works perfectly.

  7. #7
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: A Simple Email Validation Question

    Mod bump,,,,,

  8. #8
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: A Simple Email Validation Question

    If you only want to highlight,you could use Conditional Formatting also.
    Use
    =NOT(ISNUMBER(FIND("@",A1)))
    as formula for the CF

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1