find loop delete row

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
183
Office Version
  1. 2019
Platform
  1. Windows
I currently have this working, but it has to search a few 1000 rows and it takes a fair while to run. I'm hoping it can work quicker using find but can't work out how to convert it.
if theres a match its normally 5-12 rows that will match

i want to find the rows that contain wsSource.Range("C3").Value in wsDest column A
on a match delete the matched row then look for another match.

VBA Code:
                        Dim i As Long
                        
                        With wsDest
                            For i = wsDest.Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
                                If wsDest.Cells(i, 1) = wsSource.Range("C3").Value Then .Rows(i).Delete
                            Next i
                        End With
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi B-Man,

any chance to use the AutoFilter for this and delete the rows that match at once?

Holger
 
Upvote 0
Let the sheet names are Source , Dest.
VBA Code:
Sub DelRows()
Dim M
Dim Lr

'let the sheet names are Source , Dest
Lr = Sheets("Dest").Range("A" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(Dest!A1:A" & Lr & "=Source!C3,""A""&Row(Dest!A1:A" & Lr & "),False))"), False, False)
If UBound(M) >= 0 Then Sheets("Dest").Range(Join(M, ",")).EntireRow.Delete
End Sub
 
Upvote 0
Let the sheet names are Source , Dest.
VBA Code:
Sub DelRows()
Dim M
Dim Lr

'let the sheet names are Source , Dest
Lr = Sheets("Dest").Range("A" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(Dest!A1:A" & Lr & "=Source!C3,""A""&Row(Dest!A1:A" & Lr & "),False))"), False, False)
If UBound(M) >= 0 Then Sheets("Dest").Range(Join(M, ",")).EntireRow.Delete
End Sub

That will likely fail. It does depend on how many rows it finds to delete but there is a limit on the length of the address string in this part of the code.
Sheets("Dest").Range(Join(M, ",")).EntireRow.Delete

OP says a few thousand rows. With my sample data with just 2,000 rows where only 1 in 20 rows needed to be deleted the code errored on that line due to the address string length.

That would work.
Should be fast too
Will look into it
This should be much faster than AutoFilter and Delete, particularly if you have quite a few disjoint rows to delete.

I have also called my sheets "Source" and "Dest".

VBA Code:
Sub Del_Rws()
  Dim a As Variant, b As Variant, Val As Variant
  Dim nc As Long, i As Long, k As Long
 
  Val = Sheets("Source").Range("C3").Value
  With Sheets("Dest")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = Val Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A1").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
End Sub
 
Upvote 0
Solution
That will likely fail. It does depend on how many rows it finds to delete but there is a limit on the length of the address string in this part of the code.
Sheets("Dest").Range(Join(M, ",")).EntireRow.Delete

OP says a few thousand rows. With my sample data with just 2,000 rows where only 1 in 20 rows needed to be deleted the code errored on that line due to the address string length.


This should be much faster than AutoFilter and Delete, particularly if you have quite a few disjoint rows to delete.

I have also called my sheets "Source" and "Dest".

VBA Code:
Sub Del_Rws()
  Dim a As Variant, b As Variant, Val As Variant
  Dim nc As Long, i As Long, k As Long
 
  Val = Sheets("Source").Range("C3").Value
  With Sheets("Dest")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = Val Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A1").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
End Sub
Ok thanks I'll try that
 
Upvote 0
If code in post#3 is not working then try,
VBA Code:
Sub DelRows()
Dim M
Dim Lr As Long, T As Long, Str As String
'Let the sheet names are Source , Dest
Application.ScreenUpdating = False
Lr = Sheets("Dest").Range("A" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(Dest!A1:A" & Lr & "=Source!C3,""A""&Row(Dest!A1:A" & Lr & "),False))"), False, False)
For T = UBound(M) To 0 Step -1
Str = Str & "," & M(T)
If T = 0 Or T Mod 20 = 0 Then
Sheets("Dest").Range(Mid(Str, 2)).EntireRow.Delete
Str = ""
End If
Next T
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That will likely fail. It does depend on how many rows it finds to delete but there is a limit on the length of the address string in this part of the code.
Sheets("Dest").Range(Join(M, ",")).EntireRow.Delete

OP says a few thousand rows. With my sample data with just 2,000 rows where only 1 in 20 rows needed to be deleted the code errored on that line due to the address string length.


This should be much faster than AutoFilter and Delete, particularly if you have quite a few disjoint rows to delete.

I have also called my sheets "Source" and "Dest".

VBA Code:
Sub Del_Rws()
  Dim a As Variant, b As Variant, Val As Variant
  Dim nc As Long, i As Long, k As Long
 
  Val = Sheets("Source").Range("C3").Value
  With Sheets("Dest")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = Val Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A1").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
End Sub
worked a treat thanks.
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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