Timing Delays in VBA Timing Delays in VBA vba vba

Timing Delays in VBA


If you are in Excel VBA you can use the following.

Application.Wait(Now + TimeValue("0:00:01"))

(The time string should look like H:MM:SS.)


I use this little function for VBA.

Public Function Pause(NumberOfSeconds As Variant)    On Error GoTo Error_GoTo    Dim PauseTime As Variant    Dim Start As Variant    Dim Elapsed As Variant    PauseTime = NumberOfSeconds    Start = Timer    Elapsed = 0    Do While Timer < Start + PauseTime        Elapsed = Elapsed + 1        If Timer = 0 Then            ' Crossing midnight            PauseTime = PauseTime - Elapsed            Start = 0            Elapsed = 0        End If        DoEvents    LoopExit_GoTo:    On Error GoTo 0    Exit FunctionError_GoTo:    Debug.Print Err.Number, Err.Description, Erl    GoTo Exit_GoToEnd Function


You can copy this in a module:

Sub WaitFor(NumOfSeconds As Long)Dim SngSec as LongSngSec=Timer + NumOfSecondsDo while timer < sngsecDoEventsLoopEnd sub

and whenever you want to apply the pause write:

Call WaitFor(1)

I hope that helps!