How to delete multiple rows fast based on multiple conditions in column A and B?

VBA_Cancer

New Member
Joined
Nov 6, 2017
Messages
17
So I got a code that deletes rows based on 2 column conditions. However, it's very slow.
I have about 60, 000 rows and this takes forever.

VBA Code:
Sub ClearOldest()
    OldestWeek = 34
    OldestYear = 2018

    i = 2

    While Sheet5.Cells(i, "Q") = OldestWeek And Sheet5.Cells(i, "R") = OldestYear
   
        Sheet5.Rows(i).EntireRow.Delete
   
    i = i + 1
    Wend
   
    MsgBox "Mission Accomplished"
End Sub

My data looks something like this:

weekyeardata1data2
3320181335353
3320182323353
3420183522333533
3420183533535

Week number and year is sorted by oldest to newest.


Is there a much faster method?

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Add at the beginning of your code

VBA Code:
Application.ScreenUpdating=False

and add to last line before End Sub this line

VBA Code:
Application.ScreenUpdating=True

and see if that speeds things up.
 
Upvote 0
Solution
I'm so retarded, I could've done this

VBA Code:
Sub ClearOldest()
Application.ScreenUpdating = False

    OldestWeek = 34
    OldestYear = 2018
 
    i = 2
    While Sheet5.Cells(i, "Q") = OldestWeek And Sheet5.Cells(i, "R") = OldestYear
    i = i + 1
    Wend
    
    Sheet5.Rows("2:" & CStr(i)).EntireRow.Delete
Application.ScreenUpdating = False
    
    MsgBox "Mission Accomplished"
End Sub

I just find all the rows and delete them all at once. And your screen updating method helped too. Thanks
 
Upvote 0
Change the last line to True not False. You want it to repaint the screen after the Macro runs.
 
Upvote 0

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