2016-12-27 179 views
0

我有這樣一個定時計數器:Creaing與毫秒的VBA定時器計數器在Excel 2007

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 

一秒的計時器遞增,但我喜歡它顯示毫秒以及

回答

0

TimeValue()函數只能計數秒。正如我所看到的,您正在使用Application.OnTime函數等待1秒。你也可以通過Sleep()來實現這一點,你可以從內核庫中獲得。試試這個(只適用於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