Slow code

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
Office Version
  1. 2019
Platform
  1. Windows
I have this code that i am running that works, but this section is taking about 12 seconds to run, its deleting around 12k rows.

Is there a way to speed it up?

VBA Code:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

Dim we as worksheet

' Filter column C for "Yes"
ws.Range("C1").AutoFilter Field:=3, Criteria1:="Yes"

' Delete filtered rows
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
ws.Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete

' Turn off filter
ws.AutoFilterMode = False

Application.ScreenUpdating = true
Application.DisplayAlerts = true
Application.EnableEvents = true
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this. I made a small change to your code.
VBA Code:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

Dim we as worksheet
Dim myRange as Range

' Filter column C for "Yes"
ws.Range("C1").AutoFilter Field:=3, Criteria1:="Yes"

' Delete filtered rows
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set myRange = ws.Cells("C2:C" & lastRow)
myRange.Delete

' Turn off filter
ws.AutoFilterMode = False

Application.ScreenUpdating = true
Application.DisplayAlerts = true
Application.EnableEvents = true
 
Upvote 0
Try this. I made a small change to your code.
VBA Code:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

Dim we as worksheet
Dim myRange as Range

' Filter column C for "Yes"
ws.Range("C1").AutoFilter Field:=3, Criteria1:="Yes"

' Delete filtered rows
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set myRange = ws.Cells("C2:C" & lastRow)
myRange.Delete

' Turn off filter
ws.AutoFilterMode = False

Application.ScreenUpdating = true
Application.DisplayAlerts = true
Application.EnableEvents = true
it didnt work, when filered the next row after C1 was like C42, i think that stopped the code
 
Upvote 0
Try the easy thing first. Add this to the beginning
VBA Code:
    Application.Calculation = xlCalculationManual


and this to the end
VBA Code:
    Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Another one. You'll have to change sheet name possibly

VBA Code:
Sub test()
Dim sht As Worksheet, wb As Workbook, myRange As Range

Set wb = ThisWorkbook
Set sht = wb.Sheets("Sheet1")
sht.Range("C1").AutoFilter Field:=3, Criteria1:="Yes"

Set myRange = Range(sht.Cells(2, 1), sht.Cells(sht.UsedRange.Rows.Count, sht.UsedRange.Columns.Count))

Application.DisplayAlerts = False

myRange.Delete

Application.DisplayAlerts = True

End Sub
 
Upvote 0
What about using....no filter
VBA Code:
Sub DeleteRows()
  With Range("C1", Cells(Rows.Count, "C").End(xlUp))
    .Replace "Yes", "#N/A", xlWhole, , False, , False, False
   Columns("C").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
 
Upvote 0
Solution
What about a quick sort to delete all the rows to be deleted in one shot:
VBA Code:
Sub TestRowDeletes()
'
    Dim ArrayRow                As Long, NumberOfRowsToDelete   As Long
    Dim NextEmptyColumnNumber   As Long
    Dim HelperColumnArray       As Variant, YesColumnArray      As Variant
    Dim ws                      As Worksheet
'
    Set ws = Sheets("Sheet1")                                                                                                               ' <--- Set this to the name of your sheet
'
    NextEmptyColumnNumber = ws.Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1  ' Get NextEmptyColumnNumber
'
    YesColumnArray = ws.Range("C1:C" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)                                                         '
    ReDim HelperColumnArray(1 To UBound(YesColumnArray), 1 To 1)                                                                            '
'
    NumberOfRowsToDelete = 0                                                                                                                ' Reset NumberOfRowsToDelete
'
    For ArrayRow = 1 To UBound(YesColumnArray, 1)                                                                                           ' Loop thru rows of SumColumnArray
        If YesColumnArray(ArrayRow, 1) = "Yes" Then                                                                                         '   If the value is found then ...
            NumberOfRowsToDelete = NumberOfRowsToDelete + 1                                                                                 '       Increment NumberOfRowsToDelete
            HelperColumnArray(ArrayRow, 1) = 1                                                                                              '       Set row in HelperColumnArray = 1
        End If
    Next                                                                                                                                    ' Loop back
'
    If NumberOfRowsToDelete > 0 Then                                                                                                        ' If there are rows to be deleted then ...
        With ws.Range("A1").Resize(UBound(YesColumnArray), NextEmptyColumnNumber)                                                           '   Set range for possible deletion of rows
            .Columns(NextEmptyColumnNumber).Value = HelperColumnArray                                                                       '       Write the HelperColumnArray to the EmptyColumn
            .Sort Key1:=.Columns(NextEmptyColumnNumber), Order1:=xlAscending, Header:=xlNo                                                  '       Sort the Rows with '1's to the top
            .Resize(NumberOfRowsToDelete).EntireRow.Delete                                                                                  '       Delete the rows with '1's all at once
        End With
    End If
End Sub
 
Upvote 0
What about using....no filter
VBA Code:
Sub DeleteRows()
  With Range("C1", Cells(Rows.Count, "C").End(xlUp))
    .Replace "Yes", "#N/A", xlWhole, , False, , False, False
   Columns("C").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
this removed around 4 seconds from the code, so thank you!
 
Upvote 0
FWIW, a few years ago I did some testing for deletion speed using the filter approach. What I found that for data sets of ~25,000 rows or less the filter method was faster. Beyond 25K it was slower than other methods. I did some testing with your code and for me it will delete 12K rows in under one second. But that is for static data with no formulas or lookups.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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