+ Reply to Thread
Results 1 to 9 of 9

Delete rows that do not contain an email address

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2012
    Location
    Dallas, USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Delete rows that do not contain an email address

    Have a sheet with colunms First, Last, Address, City, State, Zip, Email. Some cells in the Email column contain a random value i.e. Z9A1BNSZ.

    Looking for a Macro that will delete all rows (records) that do not contain proper email addresses i.e. email@email.com.

    Any help would be appreciated.

    Thanks.

    Davbuf

  2. #2
    Registered User
    Join Date
    11-26-2012
    Location
    Kitchener, Ontario, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Delete rows that do not contain an email address

    Tri this
    Public Sub Test()
        Dim eRange As Range
        Dim Bool As Boolean
        Dim i As Long
        Bool = True
        With Sheet1
            For i = 1 To .Rows.Count
                If InStr(1, .Cells(i, "A").Value, "@") > 2 Then
                    If Bool = True Then
                        Bool = False
                        Set eRange = Rows(i)
                    Else
                        Set eRange = Union(eRange, Rows(i))
                    End If
                End If
            Next i
            eRange.EntireRow.Delete
        End With
    End Sub
    aelgadi

    > Click Star if I helped. Thanks

  3. #3
    Registered User
    Join Date
    08-21-2012
    Location
    Dallas, USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Delete rows that do not contain an email address

    Thanks aelgadi. I'm getting this error:

    Run-time error '91':
    Object variable or With block variable not set

  4. #4
    Registered User
    Join Date
    11-26-2012
    Location
    Kitchener, Ontario, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Delete rows that do not contain an email address

    that means didn't find any cell string that have "@". Have you adjusted range in the above macro cause mine assuming Column "A"

  5. #5
    Registered User
    Join Date
    08-21-2012
    Location
    Dallas, USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Delete rows that do not contain an email address

    Ok aelgadi. The email addressess are in Column G. I made the adjustment however the result was that the email addresses in column G simply lost all characters after the @. For example email1@email.com turns into email1. Random values still remain (BPX2BFFU).

  6. #6
    Registered User
    Join Date
    11-26-2012
    Location
    Kitchener, Ontario, Canada
    MS-Off Ver
    Excel 2010
    Posts
    82

    Re: Delete rows that do not contain an email address

    This explains it
    Public Sub Test()
        Dim eRange As Range
        Dim Bool As Boolean
        Dim NotFound As Boolean
        Dim i As Long
        Bool = True
        NotFound = False
        With Sheet1
            For i = 1 To .Rows.Count
                If InStr(1, .Cells(i, "A").Value, "@") > 2 Then
                    If Bool = True Then
                        Bool = False
                        Set eRange = Rows(i)
                        NotFound = True
                    Else
                        Set eRange = Union(eRange, Rows(i))
                        NotFound = True
                    End If
                End If
            Next i
            If NotFound = True Then
                eRange.EntireRow.Delete
            Else
                MsgBox "No e-mail found."
            End If
        End With
    End Sub

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete rows that do not contain an email address

    Assuming the data you wish to delete is in column A

    Sub InsertRows()
    Dim lr As Long, i As Long
    
        Application.ScreenUpdating = False
        lr = Range("A" & Rows.Count).End(xlUp).Row
        
        For i = lr To 2 Step -1
            If InStr(Cells(i, "A"), "@") = 0 Then Rows(i).delete
        Next i
        
        Application.ScreenUpdating = True
    
    End Sub

  8. #8
    Registered User
    Join Date
    08-21-2012
    Location
    Dallas, USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Delete rows that do not contain an email address

    That worked AB33. Thanks! Thank you as well aelgadi.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete rows that do not contain an email address

    You are welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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