Compare and delete rows not matching

ngocanh87

Board Regular
Joined
Mar 16, 2016
Messages
85
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi all,

I have code compare rows Result sheet & DLV030 sheet then delete row not matching but problem if at A2 (Result sheet) have only 1row, it will error at. Can help me fix this error. Thank all
d(itm) = 1
1681893040684.png

Sub save_as_file()
Dim d As Object
Dim a As Variant, b As Variant, itm As Variant
Dim nc As Long, i As Long, k As Long
Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Application.DisplayAlerts = False
Sheet1.Activate
Set d = CreateObject("Scripting.Dictionary")
a = Sheets("Result").Range("A2", Sheets("Result").Range("A" & Rows.Count).End(xlUp)).Value
For Each itm In a
d(itm) = 1
Next itm
With Sheets("DLV030")
a = .Range("AU2", .Range("AU" & Rows.Count).End(xlUp)).Value
ReDim b(1 To UBound(a), 1 To 1)
For i = 1 To UBound(a)
If Not d.exists(a(i, 1)) Then
k = k + 1
b(i, 1) = 1
End If
Next i
If k > 0 Then
Application.ScreenUpdating = False
nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
With .Range("A2").Resize(UBound(a), nc)
.Columns(nc).Value = b
.Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
.Resize(k).EntireRow.Delete
End With
Application.ScreenUpdating = True
End If
End With
ActiveSheet.Cells(Rows.Count, "D").End(xlUp).EntireRow.Delete

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Check the code that I updated in this thread:


I also put here the updated code to your sheets and your columns.
Rich (BB code):
Sub Delete_Row_1()
  Dim arr As Variant, i As Long, j As Long, lr As Long
  Dim a As Variant, b As Variant, r As Range
  Dim sh1 As Worksheet, sh2 As Worksheet, exists As Boolean
 
  Application.ScreenUpdating = False
  '
  Set sh1 = Sheets("DLV030")
  Set sh2 = Sheets("Result")
  If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
  lr = sh1.Range("AU" & Rows.Count).End(xlUp).Row
  a = sh1.Range("AU2:AU" & lr).Value2
  
  b = sh2.Range("A2:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row + 1).Value
  Set r = sh1.Range("A" & lr + 1)
  For i = 1 To UBound(a)
    exists = False
    For j = 1 To UBound(b) - 1
      If a(i, 1) Like "*" & b(j, 1) & "*" Then
        exists = True
        Exit For
      End If
    Next
    If exists = False Then Set r = Union(r, sh1.Range("A" & i + 1))
  Next
  r.EntireRow.Delete
End Sub

The code works for 1 criteria or more criteria, but if you are always going to have one criteria, you only have to filter by data different from one criteria and delete, So, this is enough:
VBA Code:
Sub Macro1()
  With Sheets("DLV030")
    .Range("AU1", .Range("AU" & Rows.Count).End(3)).AutoFilter 1, "<>" & Sheets("Result").Range("A2").Value
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .AutoFilterMode = False
  End With
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top