Excel VBA Wait Function: How to Pause a Macro
In this article
Excel VBA has no built-in Wait function, but you can pause a macro reliably with Application.Wait or the Windows API Sleep function. The quickest working method is Application.Wait Now + TimeValue("00:00:05"), which halts the macro for five seconds while Excel remains responsive. This guide shows both approaches, explains when each is appropriate, and covers the mistakes that make a pause behave unexpectedly.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- A macro-enabled workbook (.xlsm) so your code is saved.
- Basic familiarity with the VBA editor (Alt+F11) and inserting a module.
- For the Sleep method: a 64-bit or 32-bit Windows installation of Office. The Sleep API is Windows-only and will not run on Excel for Mac.
Step-by-Step
- Open the VBA editor. Press Alt+F11. In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
- Write a simple pause with Application.Wait. Type the following procedure:
This uses the current time plus a five-second offset. The macro resumes after that moment.Sub PauseFiveSeconds() MsgBox "Starting pause" Application.Wait Now + TimeValue("00:00:05") MsgBox "Five seconds have passed" End Sub - Run the macro. Place your cursor inside the procedure and press F5, or use Developer > Macros and select PauseFiveSeconds. You should see the first message box, a five-second gap, then the second message box.
- Understand the limitation of Application.Wait. While waiting, Excel does not process other events. Keystrokes and clicks are queued and handled after the wait ends. This is fine for short pauses in a batch macro, but it is not a true idle state.
- Use a loop for a more responsive wait (optional). If you need Excel to remain responsive, combine
DoEventswith a timer loop:Sub ResponsiveWait() Dim endTime As Double endTime = Timer + 5 Do While Timer < endTime DoEvents Loop End SubDoEventslets Excel handle other messages during the loop. Note thatTimerresets at midnight, so avoid this method around midnight or add handling for the rollover. - Add the Sleep API for sub-second precision (Windows only). At the top of the module, before any procedures, add:
Then call it in a procedure:#If VBA7 Then Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) #Else Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) #End If
TheSub PauseWithSleep() Sleep 2000 ' pauses for 2000 milliseconds (2 seconds) End Sub#If VBA7block keeps the declaration compatible with both older and current Office versions. - Test and save. Run each procedure once. Save the workbook as .xlsm. If you share the file with Mac users, keep the Sleep calls out of the main code path or wrap them in conditional compilation.
Common Mistakes
- Using
Waitas a function. There is noWait()function returning a value.Application.Waitis a method and must be called as a statement. - Passing a number instead of a time.
Application.Wait 5does not mean five seconds; it interprets the value as a date/time serial. Always useNow + TimeValue("00:00:05")or a similar expression. - Forgetting the date rollover.
Now + TimeValue(...)handles midnight correctly, but a bareTimerloop does not. UseNowor add rollover logic if the macro may run overnight. - Declaring Sleep without PtrSafe. On 64-bit Office, a missing
PtrSafecauses a compile error. Use the conditional block shown above. - Expecting Sleep to keep Excel responsive. Sleep blocks the entire thread, including Excel's message processing. If you need responsiveness, use the
DoEventsloop instead. - Relying on Sleep on Mac. The
kernel32library does not exist on macOS. Macros containing Sleep will fail there; useApplication.Waitor aDoEventsloop for cross-platform code.
Tips
- Keep pauses short. Long waits make the workbook appear frozen and can trigger user frustration or timeouts in automated environments.
- Wrap wait logic in a helper procedure such as
PauseSeconds(n As Double)so you can change the method in one place. - Use
Application.Waitwhen you need a precise wall-clock pause and do not care about responsiveness; use theDoEventsloop when the user must be able to interact. - If you are waiting for an external process (a file copy, a web query), consider polling a condition instead of a fixed delay: loop until the file exists or the query completes, with a timeout.
- Add
Application.StatusBarmessages before and after a wait so users know the macro is still running.
Troubleshooting
- The macro appears to hang. A long
Application.WaitorSleepblocks input. Press Ctrl+Break to interrupt, then shorten the pause or switch to theDoEventsloop. - Compile error: The code in this project must be updated for use on 64-bit systems. Add
PtrSafeto the Declare statement, or use the conditional#If VBA7block. - Runtime error 438 or "Object doesn't support this property or method". Check that you wrote
Application.Wait, notWorksheet.WaitorWaitalone. - The pause is longer or shorter than expected. Confirm the time string uses
hh:mm:ssformat and that you are adding toNow, not to a date serial. For Sleep, remember the argument is milliseconds. - Nothing happens on Mac. Remove or conditionally compile the Sleep declaration. Use
Application.Waitfor a simple cross-platform pause. - Excel becomes unresponsive during a wait. This is expected with Wait and Sleep. If responsiveness matters, replace the pause with a
DoEventsloop or restructure the macro to avoid waiting.
Frequently Asked Questions
Is there a built-in Wait function in VBA?
No. VBA provides the Application.Wait method and the Windows API Sleep function. There is no Wait() function that returns a value.
How do I pause for less than one second?
Application.Wait works in whole seconds because it compares against the system clock, which has one-second resolution in this context. For sub-second pauses, use the Windows API Sleep with a millisecond value, or a tight DoEvents loop with a high-resolution timer.
Does Application.Wait keep Excel responsive?
No. During Application.Wait, Excel does not process other events. Clicks and keystrokes are queued until the wait ends. Use a DoEvents loop if you need the interface to stay responsive.
Can I use Sleep on Excel for Mac?
No. The Sleep function comes from the Windows kernel32 library and is not available on macOS. Use Application.Wait or a DoEvents loop for cross-platform macros.
Why does my macro freeze for much longer than expected?
Check the time expression. A common error is passing a number such as 5 to Application.Wait, which is interpreted as a date serial rather than five seconds. Use Now + TimeValue("00:00:05").
Can I cancel a wait early?
Not directly. Application.Wait and Sleep run to completion unless interrupted with Ctrl+Break. If you need a cancellable pause, build a DoEvents loop that checks a flag or a cell value each iteration.