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)|
- Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
- More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
- Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
- Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
- Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
- Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
- More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
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.