Application.SendKeys宏运行得太慢。如何提高它的速度?

时间:2017-06-29 02:52:26

标签: excel vba performance excel-vba time

Thanks to A.S.H I am using this below code to open Contains and Contains Custom Filter on my Table:

Sub showContains()
Dim ws1 As Worksheet, LastRowsOfTable As Long

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

Set ws1 = Sheet1
ws1.Activate
ws1.Range("C1").Select       ' <-- customize to your case
LastRowsOfTable = ws1.Cells(Rows.Count, "F").End(xlUp).Row
ActiveWindow.ScrollRow = LastRowsOfTable - 25
        Application.SendKeys "%{DOWN}fa{Tab}{Tab}C{Tab}"

'Select C1 => click ALT+Down Arrow => text filters => contains => and contains

 With Application
     .ScreenUpdating = True
     .Calculation = xlCalculationAutomatic
     .EnableEvents = True
         End With
    End Sub

现在我的问题是,虽然我在开始时禁用了应用程序属性,但它仍然一步一步地运行宏,就像我没有禁用应用程序属性一样。它太慢了。 (我的表有8000行,但我认为不是因为这个原因。)

如何改善代码以更快地运行?

P.S:当我尝试用以下方式筛选它的速度时:

Dim starttime As Double
starttime = Timer
.
.
above codes
.
.
MsgBox Format(Timer - starttime, "00.00") & "seconds"
End Sub

Msgbox只是向我显示00.36秒这种短时间但它没有打开我的包含和包含自定义过滤器。所以看起来它的工作速度太快但实际上并没有运行任何东西。 (如果我添加此计时器代码)

通常情况下,宏会在5-7秒内运行。这种情况无法接受。

0 个答案:

没有答案