Most of the solutions presented use Application.Wait, which does not take into account the time (milliseconds) that has already elapsed since the start of the second count, so they are inaccurate up to 1 second .
A timer approach is the best solution , but you should consider resetting at midnight, so here is a very accurate Sleep method using Timer:
'You can use integer (1 for 1 second) or single (1.5 for 1 and a half second) Public Sub Sleep(vSeconds As Variant) Dim t0 As Single, t1 As Single t0 = Timer Do t1 = Timer If t1 < t0 Then t1 = t1 + 86400 'Timer overflows at midnight DoEvents 'optional, to avoid excel freeze while sleeping Loop Until t1 - t0 >= vSeconds End Sub
USE THIS TO CHECK ANY SLEEP FUNCTION: (open debug Immediate window: CTRL + G)
Sub testSleep() t0 = Timer Debug.Print "Time before sleep:"; t0 'Timer format is in seconds since midnight Sleep (1.5) Debug.Print "Time after sleep:"; Timer Debug.Print "Slept for:"; Timer - t0; "seconds" End Sub
cyberponk Nov 20 '18 at 11:52 2018-11-20 11:52
source share