My Row delete code is soooooo slow......... need cleanup and/or modification please

Javi

Active Member
Joined
May 26, 2011
Messages
440
Hi All,

The two codes below delete each EntireRow that has "Function carried out" in column I of its row. However, the sheet I am using this code on has approximately 40,000 lines occupied. Code 1 is a little faster than Code 2 but both of them are so slow there almost unusable.

Any help or assistance in speeding up this would be greatly appreciated.



Code 1

Code:
Sub SelRows_After1()
    Dim ocell As Range
    Dim rng As Range
    
    For Each ocell In Range("I3:I40000")
        
        If ocell.Value = "Function carried out" Then
            
            If rng Is Nothing Then
                
                Set rng = ocell.EntireRow
            Else
                
                Set rng = Union(rng, ocell.EntireRow)
            End If
        End If
    Next
    
     
    If Not rng Is Nothing Then rng.Delete
    
    Set rng = Nothing
    Set ocell = Nothing
End Sub


Code 2

Code:
Sub DelRow_FCO()




Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False




Dim DeleteRow As Long
Dim ws As Worksheet




Set ws = ActiveSheet


For DeleteRow = ws.Range("I" & Rows.Count).End(xlUp).Row To 3 Step -1


If ws.Range("I" & DeleteRow).Value = "Function carried out" Then
    Rows(DeleteRow).EntireRow.Delete
    End If
    
Next DeleteRow




Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True




End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How does that value get in col I, is it by formula, or some other method?
 
Upvote 0
Turning off screen updating and automatic calculation might help.
Code:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' your code.

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
[/code

You might also consider using a filter so only the rows with 'Function carried out' are visible and then using SpecialCells to delete the visible rows.
 
Upvote 0
Exported from other program, no Formula is cells.


In that case 2 options
Code:
Sub FilterDelete()

   Dim Usdrws As Long
Application.ScreenUpdating = False
   Usdrws = Range("I" & Rows.Count).End(xlUp).Row
   Range("A2:I2").AutoFilter 9, "Function carried out"
   Range("A3:A" & Usdrws).SpecialCells(xlVisible).EntireRow.Delete
   Range("A2:I2").AutoFilter
   
End Sub

Sub ReplaceDelete()

Application.ScreenUpdating = False

   With Range("I3", Range("I" & Rows.Count).End(xlUp))
      .Replace "Function carried out", "#N/A", xlWhole, , False, , False, False
      .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
   End With
End Sub
 
Upvote 0
If you don't have any errors in that column, you can try:

Code:
Sub DeleteLines()

    Range("I:I").Replace "Function carried out", "#N/A"
    Range("I:I").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
   
End Sub
Should be very fast.
 
Upvote 0
Thank you Norie, Fluff and Eric,

The below code is the fastest of the (3) option offered and somewhat faster then the original but still very very slow.

Any other ideas out there?

Code:
Sub DelRow_FCO_4()


Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False


    Range("I:I").Replace "Function carried out", "#N/A"
    Range("I:I").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
   
   


Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
 
Upvote 0
You could speed that up if you sort the data based on col I first.
 
Upvote 0
How slow is slow?

I've just both the codes I supplied on 20,000 rows deleting 3100 rows & both took under 1.5 seconds
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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