Forum Discussion
Delete Rows if cell value in multiple columns matches on a different sheet
- Jun 06, 2023
Sub deleteentries() Dim lastrow, i, j As Long Dim k As Variant For j = 2 To 30 Step 4 lastrow = Worksheets("Inventory Tracker").Cells(1048576, j).End(xlUp).Row For i = lastrow To 4 Step -1 k = Application.Match(Worksheets("Inventory Tracker").Cells(i, j).Value, Worksheets("sheet4").Columns(2), 0) If Not (IsError(k)) Then Worksheets("Inventory Tracker").Cells(i, j).Delete Worksheets("Inventory Tracker").Cells(i, j + 1).Delete Worksheets("Inventory Tracker").Cells(i, j + 2).Delete Else End If Next i Next j End SubUnfortunately i didn't correctly understand what you want to do. Now this code checks if a serial number from the Inventory Tracker is found anywhere in column B of Sheet 4. If it's found the corresponding data is deleted in sheet Inventory Tracker.
So you want to delete cells B10, C10 and D10 of sheet "Inventory Tracker" if cell B10 of this sheet has the same value as cell B10 of Sheet 4?
And you want to delete cells F15, G15 and H15 of sheet "Inventory Tracker" if cell F15 of this sheet has the same value as cell B15 of Sheet 4?
The corresponding result should apply to columns J, K, L and N, O, P and so on in my understanding.
- sebring1983Jun 05, 2023Copper Contributor
Below is a screen shot for better visual reference, if any serial number from Sheet 4 matches a Serial Number on the Inventory Tracker sheet tables it will delete it from the Inventory Tracker.
Inventory Tracker sheet
Sheet 4
- OliverScheurichJun 05, 2023Gold Contributor
Sub deleteentries() Dim lastrow, i, j As Long For j = 2 To 30 Step 4 lastrow = Worksheets("Inventory Tracker").Cells(1048576, j).End(xlUp).Row For i = lastrow To 4 Step -1 If Worksheets("Inventory Tracker").Cells(i, j).Value = Worksheets("sheet4").Cells(i, 2).Value Then Worksheets("Inventory Tracker").Cells(i, j).Delete Worksheets("Inventory Tracker").Cells(i, j + 1).Delete Worksheets("Inventory Tracker").Cells(i, j + 2).Delete Else End If Next i Next j End SubIt should be possible to loop through the tables with this code. In the attached file i've entered sample data in tables 1, 2, 3 and 8 and the matching entries are deleted by the macro.
- sebring1983Jun 06, 2023Copper Contributor
when I ran this to test it, it did run but only one Item would delete, in the screen shot blow when I run this it will look to sheet 4 Column B but would only remove the first item I have highlighted on the screen shot of the Inventory Tracker sheet.
Inventory Tracker
Sheet 4