VBA Sleep

VBA Sleep vs VBA Wait – Usage and Differences

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...

Idle time is often wasted time, however, sometimes you just need to wait for certain events to happen before you can continue code execution. VBA extends a couple of approaches to managing your idle time – the most popular approach is the Sleep procedure. The Sleep procedure pauses code execution for a certain amount of time putting the whole processes in a type of coma. It is one of the most popular approaches to pausing code execution, and at the same time simplest one. As I will try to prove – there are better, more productive approaches to pausing your code execution or utilizing potentially application idle time.

VBA Sleep function

Let us start by introducing the VBA Sleep function. The Sleep function pauses the entire process for a certain delay specified in milliseconds.

Definition and Syntax

The Sleep function is not available by default in VBA, and has to be imported from the kernel32 library. For 64 Bit procedures we need to make sure to append the PtrSafe statement.

Parameters

milliseconds
The amount of milliseconds the process should be paused for.

VBA Sleep example

The Sleep function is pretty straightforward to use:

Other VBA Sleep examples

Sleep for 10 seconds

Sleep for 1 minute

Sleep for 1 hour

Pros and Cons

The pros of using the Sleep function:

  • Easy to use
  • Precise sleep intervals (in milliseconds)

The cons of using the Sleep function:

  • Requires importing Sleep function
  • Freezes the entire process i.e. Excel does not respond
  • There is no way to stop the Sleep function

The problem with the Sleep function is that it freezes your process entirely – preventing any input or interaction with your application (even breaking code execution). Why not use that time more productively? Or let the user cancel code execution?

VBA Application.Wait function

The VBA Application.Wait is a native VBA function that pauses code execution until a certain time is reached.

Syntax

The syntax of the VBA Application.Wait function is:

Parameters

time
The time when the function should return e.g. 15:30:00 for 3:30 pm.

VBA Application.Wait example

The Application.Wait function is similarly pretty easy to use, although a little different than what you might expect with the Sleep function. This is because Application.Wait will wait until a time is reached instead of waiting for a precise interval of time.

This approach is of course less practical as usually you want to wait simply for a precise interval of time, like say 3 seconds. That is why we need to aid ourselves with the use of either the DateAdd function or the TimeValue function:

What does the function above do? It adds 1 second to the current clock time and asks VBA to wait until that moment to return from the function. You can similarly wait for longer periods of time.

Other VBA Wait examples

Wait for 10 seconds

Wait for 1 minute

Wait for 1 hour

Wait for 1 minute 30 seconds

Wait for 1 hour 2 minutes and 30 seconds

Pros and Cons

The pros of using the Application.Wait function:

  • Fairly easy to use (a little less obvious than Sleep)
  • You can break the function at any time (does not freeze the entire process)
  • Available natively from VBA

The cons of using the Application.Wait function:

  • Allows you to wait for intervals shorter no shorter than 1 second

The Application.Wait function is a little less obvious to use, but won’t freeze your VBA Project allowing you to hit Esc at any time and resume control over your project.

Third option: VBA DoEvents

If you thought those were your only options – you were wrong. VBA thankfully allows you to also use another function called DoEvents. The function seemingly….does nothing. Seriously, it doesn’t do anything more that handle all MS Office events. Now why would we want to use the DoEvents function you might ask? Well, remember that Application.Wait does not allow you to wait for intervals shorter than 1 second? On the other hand Sleep freezes your application entirely although being more granular.

Want an example of the VBA DoEvents function?

DoEvents is especially useful when you have a lot of computations going on but want to keep your VBA Project responsive WITHOUT introducing significant (1 second) delays.

VBA DoEvents example

Usually when running a macro you will want to turn off ScreenUpdating focusing your macro only on your computations. This however may seem to your user like the application has frozen. DoEvents is to be used in pair with the ScreenUpdating property:

Usually you might want to use it like this:

Conclusions

Let’s summarize our findings:

Use Application.Wait for > 1 second intervals. Application.Wait can be used well to pause code execution without freezing the application as long as you need to pause for more that 1 second (integer values).

Use DoEvents to refresh the screen on demand. Don’t want your user to get restless? Want to pause for a minimal interval just to refresh your application screen? Don’t bother with Sleep and use DoEvents instead!

Use Sleep for precise intervals. Need to wait for 500 milliseconds – no more no less? Ok… seems like you are stuck with the Sleep function.

Related Posts