Excel VBA Wait Command: How to Pause Your Macros
In this article
To create a wait command in Excel VBA, use the Application.Wait method, which pauses macro execution until a specific time or for a set number of seconds. This is essential when your code needs to wait for an external process to finish, such as a web page loading, a database query returning results, or a system file being saved, before moving to the next line of code.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with opening the Visual Basic Editor (VBE).
Step-by-Step
- Open the Visual Basic Editor: Launch Excel and press
Alt + F11on your keyboard. This opens the VBE window where all your macro code is stored. - Insert a New Module: If you haven't already created a place for your code, go to the top menu and select
Insert > Module. This creates a blank workspace for your procedure. - Define Your Subroutine: Start your macro by typing
Sub PauseMacro()and pressing Enter. This tells Excel where the specific set of instructions begins. - Calculate the Wait Time: The
Application.Waitcommand does not take a number of seconds as a direct input; instead, it requires a specific time of day. To pause for a specific duration, you must add the desired seconds to the current time using theNowfunction. For example, to wait for 5 seconds, you would useNow + TimeValue("00:00:05"). - Write the Wait Command: Enter the following line of code inside your subroutine:
Application.Wait (Now + TimeValue("00:00:05"))This tells Excel to freeze all activity until the current clock time plus five seconds has passed. - Add Surrounding Logic: To see the wait command in action, add a message box before and after the line. Your final code should look like this:
Sub PauseMacro() MsgBox "The macro will now pause for 5 seconds." Application.Wait (Now + TimeValue("00:00:05")) MsgBox "The pause is over!" End Sub - Run and Test: Press
F5or click the green Run arrow in the toolbar. You will notice that Excel becomes unresponsive for exactly five seconds before the second message box appears.
Common Mistakes
One of the most frequent errors is attempting to pass a simple integer to the Wait command, such as Application.Wait (5). This will not work because VBA interprets the number 5 as a date/time value from the beginning of the Excel calendar (January 0, 1900), causing the macro to either error out or finish instantly without a visible pause.
Another common mistake is forgetting that Application.Wait completely freezes the Excel user interface. Users often assume they can still click cells or scroll through the sheet while the macro is "waiting." In reality, the entire application enters a "Not Responding" state. If you need the screen to update or the user to remain active, Application.Wait is the wrong tool.
Some developers also forget to account for the precision of the system clock. Application.Wait is accurate to the second. If you try to use TimeValue to pause for half a second or a millisecond, it will not work because TimeValue only accepts hours, minutes, and seconds. For sub-second precision, you would need to use the Windows API Sleep function, which is significantly more complex to implement.
Tips
To make your code more flexible, avoid hard-coding the wait time. Instead, create a variable that allows you to change the pause duration easily. For example, you can define a variable Dim waitSeconds As Integer and then use Application.Wait (Now + TimeSerial(0, 0, waitSeconds)). The TimeSerial function is often more robust than TimeValue when dealing with variables.
If you are using a wait command to allow a background process to finish, consider using a "Loop with DoEvents" instead. By using a Do While loop combined with DoEvents, you can pause the macro while still allowing Excel to refresh the screen, process mouse clicks, and prevent the "Not Responding" ghosting effect. This creates a much smoother user experience.
Always place your wait commands as late as possible in the sequence. If you have five different tasks and you put a 2-second wait after each one, you've added 10 seconds of dead time to your macro. Analyze if the wait is truly necessary or if you can use a conditional check (like checking if a cell value has changed) to trigger the next step instead of relying on a blind timer.
Troubleshooting
If your macro crashes immediately after the wait command, check for conflicting events. If you have Application.EnableEvents = False set elsewhere in your code, it can sometimes cause unexpected behavior when the system tries to resume activity. Ensure events are re-enabled before the wait ends.
If the wait command seems to be ignored and the macro skips straight to the next line, verify that your system clock is synchronized. While rare, extreme discrepancies in system time can affect how Now is calculated. More commonly, this happens if the TimeValue string is formatted incorrectly (e.g., using dots instead of colons). Ensure the format is strictly "HH:MM:SS".
If Excel completely freezes and never recovers after a wait command, you may have entered an infinite loop or a wait time that is too long. You can usually force-stop a running VBA macro by pressing Ctrl + Break or Esc repeatedly. If the application is totally locked, you will need to use the Windows Task Manager to end the Excel process, though this will result in the loss of any unsaved data.
Frequently Asked Questions
Can I use the wait command for milliseconds?
No, Application.Wait only supports precision down to the second. For millisecond pauses, you must declare the Sleep function from the kernel32 library using a PInvoke declaration at the top of your module.
Does Application.Wait stop other Excel workbooks?
Yes. Because Application.Wait pauses the entire Excel application instance, all open workbooks in that same window will be frozen until the timer expires.
What is the difference between TimeValue and TimeSerial?
TimeValue converts a string (like "00:00:05") into a time value. TimeSerial takes three separate integers (hours, minutes, seconds) and creates a time value. TimeSerial is generally preferred when using variables.
Will the screen update while the macro is waiting?
No. The screen will remain static and will not reflect any changes made to the sheet immediately before the wait command unless you use DoEvents or a different pausing method.
Can I cancel a wait command while it is running?
You cannot cancel it via a button in the sheet because the UI is frozen. You must use the Ctrl + Break keyboard shortcut to interrupt the VBA execution.