我沒有直接回答你的問題;但我認爲設置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