VBA Wait and Sleep Functions – Explained

Wait and Sleep functionality in programs is required to hold or pause the program execution for a specified time. These methods suspend all the activities of Microsoft Excel and sometimes may also prevent you from performing other operations on it until the pause is in effect.

However your computer can simultaneously process other background tasks like printing or thread ordering.

VBA Sleep and Wait Functions

Significance of Wait and Sleep Functions in VBA:

These functions can be quite useful when you need to hold the program to wait for some other process or task (not directly connected or communicable to the VBA engine) that is yet to be completed.

In such cases you can find out the maximum time required for completing such a task and then in your code you hold the execution for that amount of time.

For example: We have a VBA code that can run some other executable (.exe) and after running that executable you need to wait for that executable to complete and then continue the code. So, in such a case we know that VBA cannot directly communicate with the executable file.

Hence we can code in such a way that first we start that executable then let the code to wait for 10 seconds (max time required for this executable to run) and then continue the execution again.

VBA Wait Function in Excel:

Wait is a VBA function only available in Excel. Its syntax is as follows:


Here ‘Time’ specifies the time at which you want the macro to resume again. ‘Time’ should always be in Microsoft excel time format.

Examples of Wait Function in VBA:

Example 1: Pausing a code till 2.00 PM today.

Sub WaitTest()
MsgBox ("This application is started!")
Application.Wait "14:00:00"
MsgBox ("Excecution resumed after 2PM")
End Sub

Example 2: Pausing an application for 10 seconds.

Sub WaitTest()
MsgBox ("This application is started!")
Application.Wait (Now + TimeValue("0:00:10"))
MsgBox ("Excecution resumed after 10 Seconds")
End Sub

Example 3: Using VBA Wait function to create a program that tells time after every minute (till 10 loops).

Public Sub TalkingTime()
For i = 0 To 10
  Application.Wait (Now + TimeValue("0:01:00"))
  Application.Speech.Speak ("The Time is" & Time)
Next i
End Sub

Now let’s move to Sleep Function.

Sleep Function:

Sleep is a windows function and not a VBA Function, but you can still use this function in VBA code by calling the windows Sleep API. Actually sleep is a function present inside Windows DLL files. So, before using them you have to declare the name of API above the code in your module.

The syntax of Sleep statement is as follows:

Sleep (delay)

Here, ‘delay’ specifies the time in milliseconds till which you have to pause the execution.

Examples of Sleep Function in VBA:

Example 1: Pausing an application for 10 seconds

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems
#End If
Sub SleepTest()
MsgBox "Execution is started"
Sleep 10000 'delay in milliseconds
MsgBox "Execution Resumed"
End Sub

Example 2: Halting the code for a user defined delay by using an InputBox function.

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems
#End If
Sub SleepTest()
On Error GoTo InvalidRes
Dim i As Integer
i = InputBox("Enter the Seconds for which you need to pause the code :")
Sleep i * 1000 'delay in milliseconds
MsgBox ("Code halted for " & i & " seconds.")
Exit Sub
MsgBox "Invalid value"
End Sub

Difference between VBA Wait and Sleep Function:

The job of both of these functions is same but sleep function is not as accurate as Wait. Sleep statement depends on processor’s ticks to calculate the time delays which many vary slightly on different machines. But this is not the case with Wait Function.

The advantage of Sleep statement over Wait is that it is quite flexible as you can give the time delays in milliseconds. While in Wait function you can only delay the application by whole seconds.

So, this was all about VBA Wait and Sleep Functions. Do let us know in case you have any queries related to the topic.

About Ankit Kaul

Ankit is the founder of Excel Trick. He is tech Geek who loves to sit in front of his square headed girlfriend (his PC) all day long. :D. Ankit has a strong passion for learning Microsoft Excel. His only aim is to turn you guys into 'Excel Geeks'.


  1. Bob Newsome says:

    Very nice tip although it needs at least one clarification. Your statement “Wait function is VBA system defined function” is technically incorrect. The Excel.Application.Wait function is available in the Excel application alone. It is not a function within the VBA application and thereby not available in Access, Outlook, etc. unless the Excel.Application is instantiated in one of the other Office applications.

  2. this only works on 32 bit systems.

    on 64 bit systems, VBA generates the following Compile error:

    The code in the project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them wit the PtrSafe attribute.

    Another advantage of sleep over wait is that sleep does not require CPU cycles while Wait does.

Privacy Policy | Disclaimer | About