i want to repeat an event after a certain duration that is less than 1 second. I tried using the following code
Application.wait Now + TimeValue ("00:00:01")
But here the minimum delay time is one second. How to give a delay of say half a seond?
Use Sleep Function in VBA And then you need to make sure to append the “PtrSafe” statement if you are using 64 Bit Excel. Next, you need to call the sleep function in the code. In the end, specify the time (milliseconds) for which you want to delay the code.
The Excel VBA Wait command is an instruction that can be used to delay the running of a macro. e.g. This example pauses a running macro until 6:23 P.M. today. This example displays a message indicating whether 10 seconds have passed.
VBA DoEvents yields execution of your macro, so your computer processor will be able to simultaneously run other tasks and recognize other events. The VBA DoEvents function also enables interruption of code execution so it's easier to stop a running macro.
You can use an API call and Sleep:
Put this at the top of your module:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Then you can call it in a procedure like this:
Sub test() Dim i As Long For i = 1 To 10 Debug.Print Now() Sleep 500 'wait 0.5 seconds Next i End Sub
I found this on another site not sure if it works or not.
Application.Wait Now + 1/(24*60*60.0*2)
the numerical value 1 = 1 day
1/24 is one hour
1/(24*60) is one minute
so 1/(24*60*60*2) is 1/2 second
You need to use a decimal point somewhere to force a floating point number
Source
Not sure if this will work worth a shot for milliseconds
Application.Wait (Now + 0.000001)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With