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.
Delete rows not matching criteria on another sheet with VBA
Delete rows not matching criteria on another sheet with Kutools for Excel
Criteria(In Sheet 2) | Before(Sheet 1) | After(Sheet1) |
Delete rows not matching criteria on another sheet with VBA
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 Sub
3. 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.
Delete rows not matching criteria on another sheet with Kutools for Excel
If VBA code is a little difficult for you, you can have a try on Kutools for Excel’s Compare Ranges feature.
Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 30 days. Get it Now
1. Select the sheet which you want to delete the rows which are not matching criteria. And click Kutools > Range > Compare Cells. See screenshot:
2. In the Compare Cells 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.
Click here for more information on Kutools for Excel’s Compare Ranges.
Relative Articles:
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!