Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a time delay of less than one second in excel vba?

Tags:

excel

vba

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?

like image 580
Rito Avatar asked Sep 03 '13 23:09

Rito


People also ask

How do you put a delay in VBA?

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.

Is there a wait command in VBA?

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.

What does DoEvents do in VBA?

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.


2 Answers

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 
like image 142
Doug Glancy Avatar answered Sep 23 '22 05:09

Doug Glancy


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)  
like image 24
graham nelson Avatar answered Sep 21 '22 05:09

graham nelson