Results 1 to 13 of 13

Delete rows based in three criteria in three separate columns

Threaded View

Rabbitoh Delete rows based in three... 09-05-2020, 03:19 AM
Sintek Re: Delete rows based in... 09-05-2020, 03:23 AM
oeldere Re: Delete rows based in... 09-05-2020, 03:24 AM
Rabbitoh Re: Delete rows based in... 09-05-2020, 03:47 AM
alansidman Re: Delete rows based in... 09-05-2020, 04:00 AM
Rabbitoh Re: Delete rows based in... 09-05-2020, 07:36 AM
Sintek Re: Delete rows based in... 09-05-2020, 04:09 AM
Rabbitoh Re: Delete rows based in... 09-05-2020, 06:51 AM
Rabbitoh Re: Delete rows based in... 09-05-2020, 06:09 AM
Sintek Re: Delete rows based in... 09-05-2020, 06:56 AM
Rabbitoh Re: Delete rows based in... 09-05-2020, 07:30 AM
Sintek Re: Delete rows based in... 09-05-2020, 07:35 AM
alansidman Re: Delete rows based in... 09-05-2020, 07:44 AM
  1. #1
    Forum Contributor
    Join Date
    07-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    116

    Delete rows based in three criteria in three separate columns

    Example: I have a list of 100 names on sheet1 along with other data in other columns (A - date, B - number, C - address. D - first name, E - middle name, F- last name). and a smaller list of 10 names (A - first name, B - middle name, C - last name) on sheet2. All the names on sheet2 are considered "exceptions" and as such I use the following code to remove every row where the instance of each of them appear on sheet1.

    However, the code only works based on one criteria in one column i.e. the name in column D. What I need it to do is to work based on three criteria over three columns e.g. instead of just deleting every row in Column D of sheet1 where "John" appears, I need it to identify and delete every row where "John" appears in Column D, AND "Peter" in Column E, AND "Smith" in Column F. So three columns and three criteria to be met before the row is deleted. If "all three names" "in the same order" as they appear on sheet2 two are not present then the row is not deleted.

    The code below searches the full row to find "John" rather than specifying a specific row. I hope a codemaster can help.

    
    Sub RemoveThree()
    
        Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
        Dim LastRow As Long
    
    On Error GoTo Err_Part
        
        Application.ScreenUpdating = False
        
        With Worksheets("Sheet1")
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set rng1 = .Range("A2:A" & LastRow)
        End With
    
        Set rng2 = Worksheets("Sheet2").Range("A:A")
    
        For Each c In rng1
            If Not IsError(Application.Match(c.Value, rng2, 0)) Then
                'if value from rng1 is found in rng2 then remember this cell for deleting
                If rngToDel Is Nothing Then
                    Set rngToDel = c
                Else
                    Set rngToDel = Union(rngToDel, c)
                End If
            End If
        Next c
    
        If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete
    
        ActiveWindow.ScrollRow = 1
        GoTo Exit_Point
    
    Exit_Point:
        Range("A2").Select
        Application.ScreenUpdating = True
        Exit Sub
    
    Err_Part:
        MsgBox ("An error was encountered."), vbOKOnly, "Warning", a, a
        Range("A2").Select
        Resume Exit_Point
        
    End Sub
    Last edited by Rabbitoh; 09-05-2020 at 03:40 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. split/separate rows based on criteria and send those rows by mail
    By katu in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-09-2014, 08:59 AM
  2. Find and Delete rows based on criteria in multiple columns
    By Doctor_H in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2013, 08:03 PM
  3. [SOLVED] Delete Rows based on criteria in two columns
    By KSSLR in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-30-2013, 04:39 PM
  4. Lookup value based on 2 criteria in 2 separate columns
    By mattadler22 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2013, 05:59 PM
  5. extracting rows of data from a table that are met by criteria in two separate columns
    By markhocek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2013, 03:20 AM
  6. [SOLVED] Delete Rows Based on Data in Separate Worksheet
    By Smitty7 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-27-2012, 05:48 PM
  7. VBA code to sum a range based on criteria in two separate columns
    By new.vbacoder in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2010, 02:16 PM

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