简体   繁体   English

在Excel 2007中以毫秒为单位创建VBA计时器

[英]Creaing a VBA timer counter with milliseconds in excel 2007

I have a timer counter like this: 我有一个这样的计时器:

Dim SchedRecalc As Date

Sub Recalc()

With Sheet8.Range("A1")    
    .Value = Format(Time, "hh:mm:ss AM/PM")    
End With

Call SetTime

End Sub


Sub SetTime()

SchedRecalc = Now + TimeValue("00:00:01")    
Application.OnTime SchedRecalc, "Recalc"

End Sub


Sub Disable()

On Error Resume Next

Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False

End Sub

The timer increments by one second,but i like it to show milliseconds as well 计时器增加一秒,但我也喜欢显示毫秒

The TimeValue() function can count only seconds. TimeValue()函数只能计数秒。 As I see, you are using Application.OnTime function for waiting 1 sek. 如我所见,您正在使用Application.OnTime函数等待1秒。 You can achieve this also with Sleep() , which you can get from kernel library. 您也可以使用Sleep()从内核库中获得它。 Try this (only works on Windows): 尝试以下操作(仅适用于Windows):

Option Explicit

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub test()

    MsgBox "Wait 10 sek"
    Sleep (10000)
    MsgBox "Now wait 10 milisek"
    Sleep (10)
    MsgBox "Ok"

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM