VBA Wait & Sleep Functions – Pause / Delay VBA Code
In this Article
This tutorial will demonstrate how to pause / delay code using the Wait and Sleep functions in VBA.
When we create large VBA programs that perform a lot of calculations, or perhaps even call external program to run, we may require our VBA code to stop running for a specific length of time while the external process is taking place. VBA has a few methods available in order to achieve this.
Use of Application.Wait Method
If we need to pause our macro’s running for some time or until a specified time has been reached before executing the next step, we can use the Application.Wait method. This could be useful, for example, if we have automated a login process to a website and need to wait some seconds until the page is loaded before our macro continues running.
Wait 1 Second
Including this line below into your macro, its running will be paused for approximately 1 second:
Application.Wait (Now + TimeValue("0:00:01"))
In some cases you will need to wait until a specific time. With this line below your macro will not proceed before 9am:
Please note that the Application.Wait does not accept delays of less than 1 second.
Use of Sleep Method
If you need a more precise way of pausing your macro, you can use the Sleep method.
Sleep is a Windows API function, that is, it is not part of VBA. It can be accessed by using a special declaration statement.
If you are using the 64-bit version of Microsoft Office, you can insert the following statement into a new module or at the beginning of the module (not directly in the subroutine) you want to use the Sleep function in:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
With 32-bit version use this line:
Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
After declaring the Sleep function, you have access to it in you subroutines like this:
With this line above your macro will be paused for 10,000 milliseconds, i.e., 10 seconds.
Using a Loop with Do Events
The big disadvantage of using the Wait and Sleep methods is that the user cannot do anything in Excel while waiting for the macro to continue. A user could think that Excel has stopped responding and while the user can then use Ctl+Break to interrupt the macro, this defeats the purpose of putting a pause in the macro to begin with.
To overcome this problem, we can use a loop with a method called DoEvents.
Public Sub Test() Dim i As Long For i = 1 To 20000 Range(“A1”).Value = i DoEvents Next i End Sub
Now, while Excel is running the macro above, the user can continue to interact with Excel – we can change tabs or format cells for example – basically, the macro is continuing to run but the Excel screen is not frozen. We could use a similar loop to create a timer function in Excel and incorporate the DoEvents method in that to unfreeze the screen while the timer is running.
VBA Coding Made EasyStop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!