I have a monthly base with almost 373,000 lines. Of these, part has a low value or is blank. I'd like to erase this lines.
I have part of this code to delete those that have zero. How to create a code that joins the empty row conditions (column D) in a more agile way.
Thanks
Sub DelRowsZero()
Dim i As Long
For i = Cells(Rows.Count, "D").End(xlUp).Row To 2 Step -1
If Cells(i, "D") = 0 Then Rows(i).Delete
Next i
End Sub
How about:
This assumes that A is the longest column. If this is not always the case, use:
There's apparently an argument to be made, that deleting rows as you find them would be faster than deleting them all at once.
So I ran the below code with 36000 rows of
=RANDBETWEEN(0, 10)
in columns A and B (and then copy+paste special/values), and it completed thrice in 32 seconds and dusts.Uncommenting the
currentValue
assignment and replacing the array subscript accesses withcurrentValue
comparisons adds 2.5 seconds overhead; uncommenting theIsError
check adds another 3.5 seconds overhead - but then the code won't blow up if the checked cells have the slightest chance of containing some#REF!
or#VALUE!
error.Every time I ran it, ~4000 rows ended up being deleted.
Note:
ActiveSheet
references. The code works againstSheet2
, which is the code name forWorksheets("Sheet2")
- a globally scopedWorksheet
object variable that you get for free for any worksheet that exists at compile-time. If the sheet you're running this against exists at compile-time, use its code name (that's the(Name)
property in the Properties toolwindow / F4).Of course 375K rows will run much longer than 32-38 seconds, but I can't think of a faster solution.
I am concerned about the 375K lines, who knows how long this will take to run.
I'm curious to know if this works for others, it just uses the "replace" 0 values to blanks, then uses specialcells to delete the blank rows. My test of 38K rows takes 3 seconds.