Smart Systems for Home

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

Excel VBA Pause for 1 Second: Sleep and Wait Guide

· 6 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

To pause Excel VBA for one second, call the Windows API Sleep function with 1000 milliseconds, or use Application.Wait Now + TimeValue("00:00:01") for a pure Excel approach with no API declarations. The API method works in 1-millisecond steps and does not freeze the Excel interface; Application.Wait is simpler but can only wait in whole seconds and blocks input while it runs.

What You'll Need

  • Excel on Windows for the Sleep API method (it is a Windows library call, so it does not work on Mac Excel).
  • Access to the VBA editor: press Alt+F11, or use the Developer tab. If the Developer tab is hidden, enable it via File > Options > Customize Ribbon.
  • A macro-enabled workbook (.xlsm) if you want to keep the code after closing the file.
  • Macro security set to allow macros. In the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings), choose to enable macros, or keep your file in a trusted location.

Exact menu wording can differ slightly by Excel version and region, but the paths above are stable in current Windows versions of Excel.

Step-by-Step

These steps use the Sleep API method. Steps are written for Windows Excel; the Mac alternative is covered after them.

  1. Open your workbook and press Alt+F11 to launch the VBA editor.
  2. In the Project Explorer, right-click your workbook name and choose Insert > Module. A new blank module opens. Code placed in a standard module is visible to every procedure in the project.
  3. At the very top of the module, above any Sub, add the API declaration exactly like this:
    #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
    The VBA7 check keeps the code working in both 32-bit and 64-bit Office. The older single-line Declare Sub Sleep without PtrSafe fails to compile in 64-bit Office.
  4. Below the declaration, write a test procedure:
    Sub PauseOneSecond()
        MsgBox "Starting"
        Sleep 1000
        MsgBox "One second later"
    End Sub
    Sleep takes milliseconds, so 1000 equals one second. Use 500 for half a second or 2500 for two and a half seconds.
  5. Press F5 with the cursor inside the procedure, or use Run > Run Sub/UserForm. Confirm the first message appears, dismiss it, and check that the second message arrives about a second later.
  6. If you prefer no API call at all, replace the Sleep 1000 line with Application.Wait Now + TimeValue("00:00:01"). You can then delete the declaration. This version needs no Windows library and works on Mac Excel too, but it only accepts whole-second values and Excel is unresponsive while it waits.
  7. Save the workbook as .xlsm via File > Save As, choosing the macro-enabled format, so the module survives closing.

For a sub-second wait without the API, Application.Wait cannot do it directly, because the smallest unit it accepts is one second. A common trick is looping a short DoEvents check against Timer until the required fraction of a second has elapsed, but the timing is approximate. If you need reliable fractional timing, use Sleep.

Common Mistakes

  • Passing seconds instead of milliseconds. Sleep 1 waits one millisecond, not one second. Always multiply seconds by 1000.
  • Forgetting PtrSafe. In 64-bit Office, a declaration missing PtrSafe throws a compile error that can stop the whole project from running.
  • Putting the declaration inside a Sub. Declare statements must sit at module level, above all procedures. Placing one inside a procedure gives a compile error.
  • Declaring Sleep in more than one module. Duplicate public declarations cause an "Ambiguous name detected" error. Declare it once and call it everywhere.
  • Expecting Sleep to keep Excel responsive. Sleep suspends the calling thread; on the main thread, Excel stops responding to the interface for that duration, though the delay itself is precise.
  • Using the API on Mac. kernel32 does not exist on macOS, so the declaration fails there. Use Application.Wait instead.

Tips

  • Keep wait times short in interactive macros. Long Sleep calls make Excel look frozen, and users may try to close the workbook.
  • Store the delay in a constant, such as Const DelayMs As Long = 1000, so you can adjust it in one place.
  • If the purpose is to let other applications catch up, DoEvents in a loop is often better than a flat wait, because it lets pending messages process.
  • For waiting on something that finishes at an unknown time, such as a web query or another program writing a file, loop with a timeout rather than a fixed sleep: check the condition, sleep briefly, repeat, and stop after a maximum number of attempts.
  • Place the declaration in a dedicated module so you can reuse the same pause helper across projects without conflict.

Troubleshooting

  • "Compile error: The code in this project must be updated for use on 64-bit systems." Your declaration lacks PtrSafe, or you have the old 32-bit line alongside it. Replace it with the conditional block shown in step 3.
  • "Sub or Function not defined." The declaration is missing, is in a different module that is not compiled, or is spelled differently. Confirm it sits at the top of a standard module and matches the call.
  • The pause is far too short. You likely passed seconds. Change Sleep 1 to Sleep 1000.
  • Excel freezes entirely. Either the wait is long, or Application.ScreenUpdating and Application.EnableEvents are still off. Reset them at the end of your procedure (set them back to True) so the interface recovers.
  • Macro will not run at all. Check the Trust Center macro settings, and make sure the file is saved as macro-enabled. If the file came from elsewhere, Windows may have blocked it; unblock it in the file's properties.
  • Nothing happens when you press F5. The cursor may not be inside a procedure. Click within the Sub and End Sub lines, then run again.

Frequently Asked Questions

Can I pause for half a second in VBA?

Yes, with the Windows Sleep API: Sleep 500 waits 500 milliseconds. Application.Wait cannot do it because its smallest unit is one second.

Does Sleep work on Mac Excel?

No. Sleep calls a Windows system library. On Mac, use Application.Wait Now + TimeValue("00:00:01"), or a loop with DoEvents and Timer.

Why does Excel stop responding during the pause?

Sleep suspends the code thread that Excel runs macros on, and Application.Wait blocks all input while it counts down. Both leave the window unresponsive for their duration, which is normally fine for short waits.

Should I use Sleep or Application.Wait?

Use Sleep for millisecond precision on Windows. Use Application.Wait for simplicity, whole-second delays, or Mac compatibility. Add DoEvents if the macro must keep responding while it waits.

Is there a limit on how long I can pause?

No fixed practical limit, but long sleeps make the workbook appear hung and may trigger unresponsive-application behaviour. For waits of many seconds, add progress feedback or break the wait into shorter chunks.