VBA delete rows

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
I using the Autofilter method and I would like to change to using the loop function, as it might be faster? I have anywhere from 25,000-30,000 rows..

Trying to get Delete rows if the cell in columns C contains "Pending"

My code kinda works, but it misses a few...
VBA Code:
Sub LoopCells()

Dim cell As Range, Pending As Range
Set Pending = Range("C2:C" & Cells(Rows.Count, "C").End(xlDown).Row)

For Each cell In Pending
    If cell.Value = "Pending" Then
        cell.EntireRow.Delete
    End If
Next cell

End Sub


Another request, if possible, can I modified this to if the cell contains a date?
 
@Peter_SSs
Should the code I provided be sufficient, without the need for a helper column ??
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
@Peter_SSs
Should the code I provided be sufficient, without the need for a helper column ??
Hi Michael

Your code in deleting the relevant rows directly from their existing positions is similar to the OP's original method of 'Autofilter and delete' in that if there is a lot of disjoint row ranges to delete the deletion can be quite slow. The OP was trying to change from that method in a hope of increasing speed.

The exact result depends on what is in the sheet besides what is in column C & what is "sufficient" but note my answer to your question about using a helper column was about speed, not sufficiency. :)

The OP say there is 25,000 to 30,000 rows. If there are, say, about 10% of the rows "Pending" and they are scattered throughout those rows, your code could be quite slow, particularly if there are any formulas in the other columns. For example, I set up a sheet with 30,000 rows where column C was populated with digits 1 to 9 randomly then "1" was replaced with "Pending" & very simple formulas placed in columns A & B like this.

gd6noob.xlsm
ABC
1
211138
39116
4794
510127
68105
710127
812149
9683
10#VALUE!#VALUE!Pending
118105
1210127
138105
14572
15#VALUE!#VALUE!Pending
1611138
Sheet2
Cell Formulas
RangeFormula
A2:A16A2=C2+3
B2:B16B2=C2+5


Your code took 28+ seconds to delete the relevant rows & on the identical data mine took 0.42 seconds.
Now if those values in columns A:B were also constants rather than formula results, your code 'only' took 1.8 seconds but that is still relatively slow compared to the 0.3 seconds for my code.
 
Last edited:
Upvote 0
Peter
Thank you for the comments and insights. ....always learning... :mad:
 
Upvote 0
For big / huge data, the more non contiguous rows to delete, the slower​
and as clearing is faster than deleting the sort on a helper column - if necessary as it could be a column from the data range -​
then clearing at once the block of rows to be 'deleted' may be faster than others ways …​
Rank methods from slower to faster :​
#4 : deleting row by row​
#3 : AutoFilter & Delete (if the range is not an Excel table so SpecialCells is useless)​
#2 or 1 depending on the worksheet design and the number of rows : sort & clearing …​
Just try !​
If there are not much non contiguous rows to delete so the Autofilter & Delete method is fast enough …​
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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