Slow delete loop

Nihonjin

New Member
Joined
May 7, 2005
Messages
46
Hi Experts,

My loop deletes all rows, which do have the word table in the first column. However, this is extremely slow for worksheets with over 10000 rows.

Does anybody has a faster coding idea?

j = 1
While Sheets(m).Cells(j, 1) <> ""
If Sheets(m).Cells(j, 1) = "table" Then
Rows(j & ":" & j).Select
Selection.Delete Shift:=xlUp
j = j - 1
End If
j = j + 1
Wend

Regards,
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Less access to the object faster the process
Code:
Sub test()
Dim i As Long, txt As String
again:
For Each r In Range("a1", Range("a65536").End(xlUp)).SpecialCells(xlCellTypeConstants)
    If r = "Table" Then txt = txt & r.Row & ":" & r.Row & ","
    If Len(txt) > 240 Then
        txt = Left(txt, Len(txt) - 1)
        Range(txt).Delete: txt = Empty: GoTo again
    End If
Next
If Len(txt) > 0 Then
    txt = Left(txt, Len(txt) - 1)
    Range(txt).Delete
End If
End Sub
 
Upvote 0
Hi N,

OK, as regards generally speeding-up code its worth remembering that the 2 main culprits for making code slow are often calculation and screenupdating. Therefore, you could try switching them off at the start of the routine and then reset again at the end.

Like this:
Code:
Sub Faster()
    Dim lCalc As Long
    
    With Application
        lCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    
    '
    'do slow stuff here
    '
    
    With Application
        .Calculation = lCalc
        .ScreenUpdating = True
    End With
    
End Sub
That said, the very act of looping can be a major cause of delay - you have to check all of the rows to check whether the condition is satisfied. There are alternatives such as using Find or the AutoFilter - have a Search as this has been covered many times before.

HTH
 
Upvote 0
Thanks Jindon & Richie,

geee, yes, this has been covered over and over. Thanks for the hint. I went with this solution:

Sub del_table()

Dim EvalRange As Range

Set EvalRange = Range("A1:A" & Range("A65536").End(xlUp).Row)
EvalRange.AutoFilter Field:=38, Criteria1:="table"
EvalRange.Offset(1).Resize(EvalRange.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
EvalRange.AutoFilter

End Sub

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,239
Members
448,879
Latest member
VanGirl

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