我没有直接回答你的问题;但我认为设置Application.visible
到false
不会提升性能;我更喜欢使用以下代码:
Public Sub YK_Start()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
End Sub
Public Sub YK_End()
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
使用例如:
Sub LoopExample()
Dim Cell As Range
Call YK_Start
Columns("B:F").ClearContents
For Each Cell In Range("A1:A100000")
Cell.Offset(, 1) = Cell.Value + 1
Cell.Offset(, 2) = Cell.Value + 2
Cell.Offset(, 3) = Cell.Value + 3
Cell.Offset(, 4) = Cell.Value + 4
Cell.Offset(, 5) = Cell.Value + 5
Next Cell
Call YK_End
End Sub
此源代码将在18 seconds
而不使用Call YK_Start
和Call YK_End
执行;并且将使用这些程序在10 seconds
内执行。
参考:www.officena.net:阿拉伯语办公论坛。
编辑#1
有很多方法来衡量您的代码的执行时间;我不知道最准确的一个;我只需要近似值;请参阅:
How do you test running time of VBA code?
我用最简单的一个:
Sub my_test()
Dim t As Single
t = Timer
'code
Call LoopExample
MsgBox Timer - t
End Sub
你是如何测量运行时间的?在excel应用程序中是否有某处可以找到我的代码的运行时? –
@AdityaGuru:我修改了我的答案;请参阅**编辑#1 ** – houssam