Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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

doc-delete-based-on-another-sheet-1 doc-delete-based-on-another-sheet-2 doc-right doc-delete-based-on-another-sheet-3
 Criteria(In Sheet 2)     Before(Sheet 1)          After(Sheet1)


arrow blue right bubble 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:

doc-delete-based-on-another-sheet-4

4. Click OK, and another dialog pops out for selecting the criteria in another sheet, see screenshot:

doc-delete-based-on-another-sheet-5

5. Now all rows not matching the criteria in another sheet are removed.


arrow blue right bubble 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 > Compare Ranges. See screenshot:

doc-delete-based-on-another-sheet-6

2. In the Compare Ranges dialog, click button in the Range B, and then you can select the criteria range you need. See screenshot:

doc-delete-based-on-another-sheet-7
doc-arrow
doc-delete-based-on-another-sheet-8

 

3. Then click OK, then back to the Compare Ranges dialog, check Different Value and Select entire rows. See screenshot:

doc-delete-based-on-another-sheet-9

4. Click Ok, and exit the Compare Ranges dialog. Then you see the value rows which are different from the criteria are selected.

doc-delete-based-on-another-sheet-10

5. Right click to click Delete from the context menu to delete the rows which are not matching the criteria on another sheet.

doc-delete-based-on-another-sheet-11

Click here for more information on Kutools for Excel’s Compare Ranges.


Relative Articles:



Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: 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!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    TonyT · 3 years ago
    Doesn't work with sheets that have multiple columns because it doesn't actually delete the row, just the cell.