【问题标题】:Excel VBA Ontime function with Milliseconds [duplicate]毫秒的Excel VBA Ontime函数[重复]
【发布时间】:2021-05-01 12:50:11
【问题描述】:

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

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

Sub increment()
'some code here
startTimer
End Sub

代码运行良好,但有什么方法可以增加几分之一秒(例如:10 毫秒) ?

【问题讨论】:

标签: excel vba milliseconds


【解决方案1】:

试试这个:

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

这是受到现有答案 here 的评论的启发。

【讨论】:

    【解决方案2】:

    如果认真使用毫秒,确实在减法或比较值时,请务必使用 Decimal 而不是 Double 进行计算以避免位错误:

    Dim OneMillisecond As Variant
    
    OneMillisecond = CDec(TimeSerial(0, 0, 1) / 1000)
    ' OneMillisecond = 0.0000000115740740740741 
    
    TenMilliseconds = OneMillisecond * CDec(10)
    

    MS Access Can Handle Millisecond Time Values - Really

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 2020-10-31
      • 2016-08-25
      相关资源
      最近更新 更多