Is there better method of deleting rows in an excel spreadsheet than than what I currently use?
On a spreadsheet I run a macro that deletes a row if a figure in a particular cell is ‘0’.
Public Sub deleteRowsIfZero(colDelete As Integer)
Dim r As Long
Application.ScreenUpdating = False
For r = Cells(Rows.Count, colDelete).End(xlUp).Row To 1 Step -1
If Cells(r, colDelete).Value = "0" Then Rows(r).Delete
Next r
Application.ScreenUpdating = True
End Sub
This works, but with a spreadsheet of 700+ rows it can be quite slow. Is there a more efficient method to do this?
Thanks in advance for any advice.
Cheers
Noel
Turn off screen updating and calculation before you begin and restore those settings at the end.
Note that you are unnecessarily retesting a row every time you delete because the deletion slides the rows up. Therefore you could decrement
reach time you do a deletion for a small optimization.A further optimization would be to read in all the test values at one time. Each read/write from/to Excel/VBA has an overhead. See example below:
You can also try deleting all at once to see which is faster.