使用 Timer 查詢效能瓶頸
優化速度的第一步是找到最慢的程式碼段。Timer
VBA 函式返回自午夜以來經過的秒數,在基於 Windows 的 PC 上精度為 1/256 秒(3.90625 毫秒)。VBA 函式 Now
和 Time
僅精確到秒。
Dim start As Double ' Timer returns Single, but converting to Double to avoid
start = Timer ' scientific notation like 3.90625E-03 in the Immediate window
' ... part of the code
Debug.Print Timer - start; "seconds in part 1"
start = Timer
' ... another part of the code
Debug.Print Timer - start; "seconds in part 2"