Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Wait Function: How to Pause a Macro

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open the VBA editor. Press Alt+F11. In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
  2. Write a simple pause with Application.Wait. Type the following procedure:
    Sub PauseFiveSeconds()
        MsgBox "Starting pause"
        Application.Wait Now + TimeValue("00:00:05")
        MsgBox "Five seconds have passed"
    End Sub
    This uses the current time plus a five-second offset. The macro resumes after that moment.
  3. 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.
  4. 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.
  5. Use a loop for a more responsive wait (optional). If you need Excel to remain responsive, combine DoEvents with a timer loop:
    Sub ResponsiveWait()
        Dim endTime As Double
        endTime = Timer + 5
        Do While Timer < endTime
            DoEvents
        Loop
    End Sub
    DoEvents lets Excel handle other messages during the loop. Note that Timer resets at midnight, so avoid this method around midnight or add handling for the rollover.
  6. Add the Sleep API for sub-second precision (Windows only). At the top of the module, before any procedures, add:
    #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
    Then call it in a procedure:
    Sub PauseWithSleep()
        Sleep 2000   ' pauses for 2000 milliseconds (2 seconds)
    End Sub
    The #If VBA7 block keeps the declaration compatible with both older and current Office versions.
  7. 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 Wait as a function. There is no Wait() function returning a value. Application.Wait is a method and must be called as a statement.
  • Passing a number instead of a time. Application.Wait 5 does not mean five seconds; it interprets the value as a date/time serial. Always use Now + TimeValue("00:00:05") or a similar expression.
  • Forgetting the date rollover. Now + TimeValue(...) handles midnight correctly, but a bare Timer loop does not. Use Now or add rollover logic if the macro may run overnight.
  • Declaring Sleep without PtrSafe. On 64-bit Office, a missing PtrSafe causes 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 DoEvents loop instead.
  • Relying on Sleep on Mac. The kernel32 library does not exist on macOS. Macros containing Sleep will fail there; use Application.Wait or a DoEvents loop 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.Wait when you need a precise wall-clock pause and do not care about responsiveness; use the DoEvents loop 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.StatusBar messages before and after a wait so users know the macro is still running.

Troubleshooting

  • The macro appears to hang. A long Application.Wait or Sleep blocks input. Press Ctrl+Break to interrupt, then shorten the pause or switch to the DoEvents loop.
  • Compile error: The code in this project must be updated for use on 64-bit systems. Add PtrSafe to the Declare statement, or use the conditional #If VBA7 block.
  • Runtime error 438 or "Object doesn't support this property or method". Check that you wrote Application.Wait, not Worksheet.Wait or Wait alone.
  • The pause is longer or shorter than expected. Confirm the time string uses hh:mm:ss format and that you are adding to Now, 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.Wait for 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 DoEvents loop 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.