In VBA, the Wait method of the Application object pauses code execution until a specific time of day, while the Sleep API function pauses for a set number of milliseconds. For most simple delays, use Application.Wait with Now + TimeValue("00:00:05") to pause for 5 seconds; for finer control or non-blocking delays, use the Windows Sleep function or Application.OnTime.
What Application.Wait Actually Does
Application.Wait suspends all VBA execution until the system clock reaches the time you specify. It takes a single argument: a date/time value. Because VBA stores times as fractions of a day, you can add a duration to the current moment using TimeValue or TimeSerial.
Example – pause for 3 seconds:
Sub PauseThreeSeconds()
Application.Wait Now + TimeValue("00:00:03")
MsgBox "Done waiting"
End Sub
This works in Excel, Word, Access, and other Office applications that expose the Application object. It is the simplest method and requires no API declarations.
Using the Sleep API for Millisecond Delays
If you need a delay shorter than one second, Application.Wait is not suitable because it only resolves to the nearest second. The Windows Sleep function accepts milliseconds. You must declare it at the top of a standard module:
#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Then call it directly:
Sub PauseHalfSecond()
Sleep 500 ' 500 milliseconds
End Sub
Note: Sleep blocks the entire application (Excel becomes unresponsive) for the duration. It is fine for short pauses, but for long delays it may appear as a freeze.
Non-Blocking Delay with Application.OnTime
If you want the user to keep interacting with Excel while a delayed action runs, use Application.OnTime. It schedules a macro to run at a specific time, without halting code. This is ideal for timed refreshes or reminders.
Example – run a procedure 5 seconds from now:
Sub ScheduleRun()
Application.OnTime Now + TimeValue("00:00:05"), "MyScheduledMacro"
End Sub
Sub MyScheduledMacro()
MsgBox "5 seconds have passed"
End Sub
Remember to cancel scheduled runs if you close the workbook early, using Application.OnTime EarliestTime:=..., Procedure:="MyScheduledMacro", Schedule:=False.
How to Choose the Right Method
Your choice depends on the purpose:
- Simple whole-second delay – use
Application.Wait. - Sub-second delay – use
Sleep. - Keep UI responsive – use
Application.OnTime. - Wait for a condition (e.g., a file to appear) – use a loop with
DoEventsand a shortSleep.
Common Problems and Pitfalls
One frequent mistake is using Application.Wait with a time earlier than now, which causes an immediate return or an error. Always add to Now.
Another issue: Sleep blocks all Excel windows, so if you have multiple workbooks open, they all freeze. Also, Sleep is not available on Mac VBA without additional declarations (the Declare statement is Windows-only).
If you use Application.Wait inside a loop, it can slow down your macro significantly. For example, a loop that waits 1 second 10 times takes 10 seconds; consider restructuring to wait once.
Alternatives and Related Techniques
For timing code execution (not pausing), use Timer function to measure elapsed seconds. For example:
Sub MeasureTime()
Dim startTime As Double
startTime = Timer
' ... your code ...
MsgBox "Elapsed: " & Timer - startTime & " seconds"
End Sub
If you need to wait for a specific time of day (e.g., 9:30 AM), pass that time directly: Application.Wait #9:30:00 AM#.
Frequently Asked Questions
Can I pause VBA for 1 second?
Yes, use Application.Wait Now + TimeValue("00:00:01") or Sleep 1000. The first is simpler and works on Mac; the latter is more precise.
Why does Application.Wait not work on my Mac?
It does work on Mac for Excel 2016 and later, but the Sleep API is Windows-only. Use Application.Wait for cross-platform compatibility.
How do I pause VBA without freezing Excel?
Use Application.OnTime to schedule a macro instead of blocking. Alternatively, in a loop, call DoEvents to process user input, but that still blocks the macro.
Is there a way to wait for a specific time of day?
Yes, pass a time literal to Application.Wait, e.g., Application.Wait #12:00:00 PM# will pause until noon.
Can I use a variable for the wait duration?
Yes, build the time value: Application.Wait Now + TimeSerial(0, 0, seconds) where seconds is a Long variable.
What is the difference between Wait and Sleep?
Application.Wait is an Excel method that pauses until a specific time; Sleep is a Windows API that pauses for a set number of milliseconds. Sleep is more precise but blocks the whole application.
Important Reminder
Before running any macro that pauses or loops, save a copy of your workbook. Macros cannot be undone with Ctrl+Z, and a long wait might make you think the application has crashed. Always test with a short delay first.
Related
- GESTEP Function in Google Sheets: Syntax & Examples
- Balance Sheet vs Income Statement: Key Differences
- Voter Registration Data Entry Jobs: Real Work & Pay
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Data Entry Form Template: Build and Use OneAn Excel VBA data entry form template is a reusable UserForm that collects values, checks them, and appends…
- Macro Recorder Setup in Excel: A Step-by-Step GuideTo set up the Macro Recorder in Excel for Microsoft 365 on Windows, show the Developer tab, choose…
- VBA Is Range? What It Means in ExcelIn Excel VBA, Range is not a question but a fundamental object: it represents a cell, a row,…
- Excel VBA UserForm Cancel Button: Setup and BehaviorTo make an Excel VBA UserForm button act as Cancel, set that CommandButton’s Cancel property to True, then…
- Clear Excel VBA: Contents, Formats, and CellsTo clear cells with Excel VBA, use Range("B2:D20").ClearContents to remove values and formulas while keeping formatting, or Range("B2:D20").Clear…
- Excel VBA Data Types: Choosing the Right Variable for MacrosExcel VBA data types define what kind of value a variable can hold, such as a whole number,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.