使用 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"