Delete Rows if cell value in multiple columns matches on a different sheet

csebring1983

New Member
Joined
May 15, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am looking to delete rows from Sheet 3 in Multiple columns ( I think I might have to make tables for this ) if cell data in Columns B,F,I matches cell data from sheet 4 in column B. The VBA I have for a different sheet works but only to match 1 column as it only looks to column B on both sheets.

Sub delete_selected_rows()
Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
Dim lastRow As Long

With Worksheets("Sheet3")
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng1 = .Range("B4:B" & lastRow)
End With

Set rng2 = Worksheets("Sheet4").Range("B:B")

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
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Could you provide a copy of your Sheet3 using the XL2BB - Excel Range to BBCode, or better still, share your file via Dropbox, Google Drive or similar file sharing platform? It's difficult to write/test code when we don't know exactly how your dataset is structured. The following suggestion is based on your sheet3 looking like:
csebring1983.xlsm
ABCDEFGHIJ
1
2
3HDR1HDR2HDR3HDR4HDR5HDR6HDR7HDR8
449xxx41xx16
527xxx0xx43
68xxx0xx22
747xxx0xx0
839xxx0xx0
94xxx0xx0
1010xxx20xx0
1127xxx13xx0
1240xxx5xx0
1344xxx40xx42
14
Sheet3


When the following code is run:
VBA Code:
Option Explicit
Sub csebring1983()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet3")
    Set ws2 = Worksheets("Sheet4")
    Dim LCol As Long, LRow As Long
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    LRow = ws1.Range("B:I").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
   
    Dim a, b, c, i As Long
    a = ws1.Range("B4:I" & LRow)
    b = ws2.Range("B2", ws2.Cells(Rows.Count, "B").End(xlUp))
    ReDim c(1 To UBound(a, 1), 1 To 1)
   
    For i = LBound(a, 1) To UBound(a, 1)
        If Application.Count(Application.Match(a(i, 1), b, 0)) > 0 Or _
            Application.Count(Application.Match(a(i, 5), b, 0)) > 0 Or _
            Application.Count(Application.Match(a(i, 8), b, 0)) > 0 Then
            c(i, 1) = 1
        End If
    Next i
   
    ws1.Cells(4, LCol).Resize(UBound(c)).Value = c
    i = WorksheetFunction.Sum(ws1.Columns(LCol))
    If i > 0 Then
        ws1.Range(ws1.Cells(3, 2), ws1.Cells(LRow, LCol)).Sort Key1:=ws1.Cells(3, LCol), _
        order1:=xlAscending, Header:=xlYes
        ws1.Cells(4, LCol).Resize(i).EntireRow.Delete
    End If
End Sub
You end up with this:
csebring1983.xlsm
ABCDEFGHIJ
1
2
3HDR1HDR2HDR3HDR4HDR5HDR6HDR7HDR8
427xxx0xx43
58xxx0xx22
647xxx0xx0
74xxx0xx0
810xxx20xx0
927xxx13xx0
1044xxx40xx42
11
Sheet3


Based on your sheet4 looking like this:
csebring1983.xlsm
B
1Hdr
21
33
45
56
611
714
817
923
1025
1126
1236
1339
1449
1551
1657
1758
1863
1968
2069
2174
2277
2381
2488
2589
2690
2792
2896
2999
Sheet4


As you can imagine, there will (probably) need to be significant tweaking once the actual sheet layouts is known.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top