Speed up my Code

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
Office Version
  1. 2019
Platform
  1. Windows
Hi all-

I am running this section of code, which does a quick filter and deletes all the filtered info, which is round 20k rows. Its running really slow, this section is taking roughly 180 seconds to run. Is there a method that would speed this up at all?




VBA Code:
' Filter column N for False
    ws.Range("N1").AutoFilter Field:=14, Criteria1:="False"
    
    ' Delete all visible rows except header row
    lastRow = ws.Cells(ws.Rows.Count, "N").End(xlUp).Row
    
    Dim visibleRows As Range
    Set visibleRows = ws.Range("N2:N" & lastRow).SpecialCells(xlCellTypeVisible)
    If Not visibleRows Is Nothing Then visibleRows.EntireRow.Delete
    
    ' Remove filter
    ws.AutoFilterMode = False

thank you,
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
180 seconds to 21 seconds, just for your information!
That is a reasonable improvement but I am surprised that it still took 21 seconds. I thought that it would be much faster than that. Do you have a lot of formulas in the worksheet too?
If so, does adding these two lines where shown make any further significant improvement?

Rich (BB code):
    If k > 0 Then
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual
      With Range("A2").Resize(UBound(a), nc)
        .Columns(nc).Value = b
        .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
    End If
 
Upvote 0
Just to tidy up, my earlier code had a couple of errors in it. Those errors would only be a problem if the sheet from which the rows are being deleted is not the active sheet when the code is run.
The code was missing three "." characters - marked in red in the corrected code below.

Rich (BB code):
Sub Del_Text_FALSE()
  Dim ws As Worksheet
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
  
  Set ws = ActiveSheet '<- or whatever sheet you want
  With ws
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("N2", .Range("N" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If UCase(a(i, 1)) = "FALSE" Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual
      With .Range("A2").Resize(UBound(a), nc)
        .Columns(nc).Value = b
        .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,846
Messages
6,181,304
Members
453,031
Latest member
Chris_1

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