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











LinkBack URL
About LinkBacks


Register To Reply
Bookmarks