How to delete rows not matching criteria on another sheet?
It may be not difficult for you to delete rows which do not match the criteria on the same sheet, but do you know any ways to delete the rows not matching criteria on another sheet as shown as below. Now I have two tricks that can help you quickly solve it.
|Criteria(In Sheet 2)||Before(Sheet 1)||After(Sheet1)|
Recommended Excel Productivity Tools
There is a VBA code can help you delete rows not matching criteria on another sheet.
1. Press Alt + F11 to display the Microsoft Visual Basic for Applications window.
2. In the window, click Insert > Module to show a new module window, then copy the following VBA code into the module window.
VBA: Delete rows not matching criteria on another sheet in Excel.
Sub DeleteRow() 'Updateby20140618 Dim rng As Range Dim Rng1 As Range, Rng2 As Range Dim arr1 As Variant Dim arr2 As Variant Dim dic2 As Variant Dim OutArr As Variant xTitleId = "KutoolsforExcel" Set Rng1 = Application.Selection Set Rng1 = Application.InputBox("Range1 :", xTitleId, Rng1.Address, Type:=8) Set Rng2 = Application.InputBox("Range2:", xTitleId, Type:=8) Set Rng1 = Rng1.Columns(1) Set Rng2 = Rng2.Columns(1) Set dic2 = CreateObject("Scripting.Dictionary") arr1 = Rng1.Value arr2 = Rng2.Value For i = 1 To UBound(arr2, 1) xKey = arr2(i, 1) dic2(xKey) = "" Next Rng1.ClearContents OutArr = Rng1.Value xIndex = 1 For i = 1 To UBound(arr1, 1) xKey = arr1(i, 1) If dic2.Exists(xKey) Then OutArr(xIndex, 1) = xKey xIndex = xIndex + 1 End If Next Rng1.Value = OutArr End Sub3. Click Run, a KutoolsforExcel dialog pops up for you to select a range you want to delete the rows which are not matching criteria. See screenshot:
4. Click OK, and another dialog pops out for selecting the criteria in another sheet, see screenshot:
5. Now all rows not matching the criteria in another sheet are removed.
If VBA code is a little difficult for you, you can have a try on Kutools for Excel’s Compare Ranges feature.
1. Select the sheet which you want to delete the rows which are not matching criteria. And click Kutools > Compare Ranges. See screenshot:
2. In the Compare Ranges dialog, click button in the Range B, and then you can select the criteria range you need. See screenshot:
3. Then click OK, then back to the Compare Ranges dialog, check Different Value and Select entire rows. See screenshot:
4. Click Ok, and exit the Compare Ranges dialog. Then you see the value rows which are different from the criteria are selected.
5. Right click to click Delete from the context menu to delete the rows which are not matching the criteria on another sheet.