[VBA] Very slow looping through deleting rows

ajamess

Board Regular
Joined
Sep 13, 2016
Messages
92
I'm writing a macro to prompt for a user entered category and loop through ~ 40k rows and delete all except their category (with the largest category this would be ~8k left, so deleting 32k rows). I'm thinking there has to be a more efficient way to do this, its been running at least 7 minutes (have a timer, running, i'll post when complete)



Sub category()

Workbooks("Book1.xlsx").activate

dim category as variant

category = InputBox("What Category are you analyzing?")

Rowcount = application.worksheetfunction.countA(Range("A:A"))

For i =RowCount to 2 Step -1
If cells(i,3).value <> category Then
Rows(i).delete
End If
Next



It ended up taking 18 minutes. There has to be a way to speed this up!
 

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.
Try

Code:
Sub MM1()
Workbooks("Book1.xlsx").Activate
Application.ScreenUpdating = False
Dim lr As Long, category As Variant 'could be dimmed as string if it is text
lr = Cells(Rows.Count, "A").End(xlUp).Row
category = InputBox("What Category are you analyzing?")
For i = lr To 2 Step -1
    If Cells(i, 3).Value <> category Then
        Rows(i).Delete
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:
Code:
Sub FilterMini()
Application.ScreenUpdating = False
Workbooks("Book1.xlsx").Activate
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
Dim category As Variant
category = InputBox("What Category are you analyzing?")
    With ActiveSheet.Range("C1:C" & Lastrow)
        .AutoFilter Field:=1, Criteria1:=category, Operator:=xlFilterValues
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
@MAIT
Agreed your method will be quicker....but I'm concerned that the filter limit of 10000 uniques might come into play, especially if it's an older version of EXcel..(y)
 
Upvote 0
@MAIT
Agreed your method will be quicker....but I'm concerned that the filter limit of 10000 uniques might come into play, especially if it's an older version of EXcel..(y)


I did not know about limits:

And now that I look again it should be <> like this:

Code:
.AutoFilter Field:=1, Criteria1:="<>" & category, Operator:=xlFilterValues
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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