Excel VBA reduce processing time of Autofilter Macro

时间:2017-08-04 12:12:10

标签: excel-vba vba excel

I have a workbook with 1 worksheet that contains 14k rows and 36 columns. Columns 24 to 36 contain formulas (ao find and vlookup formulas).

The Calculation is set to manual.

I created a test macro that sets an autofilter on the dataset and I included 4 filter criteria (see code below). The strange thing is that when I run this macro for the first time it takes 109 seconds to run, the second time it takes 17 seconds, and the third and consecutive times it is around 0,3 seconds. I find this really strange. How can this happen and what can I do to get the 0,3 seconds the first time?

Thanks.

Sub test() 
Dim ws As Worksheet
Dim ws As Worksheet    
Dim rng As Range    
Dim PG()

PG = Array(“441”, “445”, “446”, “447”)
Set ws = Worksheets(“Notificaties”)
ws.AutoFilterMode = False
Set rng = ws.Range(“A1”)
Set rng = ws.Range(rng, rng.End(xlToRight))

With rng    
.AutoFilter    
.AutoFilter Field:=4, Criteria1:=”TWAP*”    
.AutoFilter Field:=29, Criteria1:=”<>*II*”    
.AutoFilter Field:=30, Criteria1:=”TRUE”    
.AutoFilter Field:=22, Criteria1:=PG, Operator:=xlFilterValues
End With

End Sub

1 个答案:

答案 0 :(得分:0)

如果公式尚未更新,则第一个autofilter命令将更新它们,即使使用Application.Calculation = xlCalculationManual也是如此。因此,在使用Autofilter之前,您可能希望使用如下命令更新公式:

Application.Calculation = xlCalculationAutomatic