Vasant,
Since the Wait method takes an Excel Date Time, I don't think it can compare
to decimal seconds. It depends on the rounded values of the current time and
expected elapsed time, and could execute anywhere between 0 and 1 second
later. It's hard to predict, but if you have a high resolution timer, then
you could test it better:
Dim oTimer As New CHiResTimer
Sub test()
oTimer.StartTimer
Application.Wait Now + (TimeValue("00:00:01") / 2)
oTimer.StopTimer
MsgBox "That took " & Format(oTimer.Elapsed, "#.000000") & " seconds."
oTimer.StartTimer
Application.Wait Now + (TimeValue("00:00:01"))
oTimer.StopTimer
MsgBox "That took " & Format(oTimer.Elapsed, "#.000000") & " seconds."
oTimer.StartTimer
Application.Wait Now + (TimeValue("00:00:02"))
oTimer.StopTimer
MsgBox "That took " & Format(oTimer.Elapsed, "#.000000") & " seconds."
End Sub
Put the code below into a class module named CHiResTimer, and the macro
above will show the time differences.
HTH,
Bernie
MS Excel MVP
Option Explicit
'How many times per second is the counter updated?
Private Declare Function QueryFrequency Lib "kernel32" _
Alias "QueryPerformanceFrequency"
( _
lpFrequency As Currency) As Long
'What is the counter's value
Private Declare Function QueryCounter Lib "kernel32" _
Alias "QueryPerformanceCounter" ( _
lpPerformanceCount As Currency) As
Long
'Variables to store the counter information
Dim cFrequency As Currency
Dim cOverhead As Currency
Dim cStarted As Currency
Dim cStopped As Currency
Private Sub Class_Initialize()
Dim cCount1 As Currency, cCount2 As Currency
'Get the counter frequency
QueryFrequency cFrequency
'Call the hi-res counter twice, to check how long it takes
QueryCounter cCount1
QueryCounter cCount2
'Store the call overhead
cOverhead = cCount2 - cCount1
End Sub
Public Sub StartTimer()
'Get the time that we started
QueryCounter cStarted
End Sub
Public Sub StopTimer()
'Get the time that we stopped
QueryCounter cStopped
End Sub
Public Property Get Elapsed() As Double
Dim cTimer As Currency
'Have we stopped or not?
If cStopped = 0 Then
QueryCounter cTimer
Else
cTimer = cStopped
End If
'If we have a frequency, return the duration, in seconds
If cFrequency > 0 Then
Elapsed = (cTimer - cStarted - cOverhead) / cFrequency
End If
End Property