首页 > 解决方案 > 以毫秒为单位的 Excel VBA Ontime 函数

问题描述

我编写了这段代码,使用 ontime 函数每秒运行一个过程:

Sub startTimer()
 Application.OnTime (Now + TimeValue("00:00:01")), "increment"
End Sub

Sub increment()
'some code here
startTimer
End Sub

代码工作正常,但是有没有办法增加几分之一秒(例如:10 ms)?

标签: excelvbamilliseconds

解决方案


尝试这个:

Sub startTimer()
    
    Const MILLISECOND_MULTIPLIER As Double = 0.000000011574;
    
    ' bracket is optional, but kept for clarity
    Application.OnTime (Now + (MILLISECOND_MULTIPLIER * 10)), "increment")
    
End Sub

这受到来自现有答案的评论的启发


推荐阅读