Smart Systems for Home

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

Excel VBA Wait Loop: Pause Code Until a Condition Is Met

· 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 make Excel VBA wait and then continue, use a Do While or Do Until loop around Application.Wait or Sleep, or poll a condition with DoEvents inside the loop. The shortest working pattern is Do While [condition]: DoEvents: Loop for waiting on a cell value, or Application.Wait Now + TimeValue("00:00:05") for a fixed pause. The rest of this guide shows both approaches, how to avoid freezing Excel, and how to stop a loop that never ends.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A macro-enabled workbook (.xlsm) so your code is saved.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on many Macs).
  • For the Sleep function: a Declare statement at the top of a standard module. On Mac, Sleep is not available the same way; use Application.Wait instead.

Menu labels and the exact location of options can differ slightly by Excel version, platform and region.

Step-by-Step

  1. Open the VBA editor and insert a module. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac), then in the Project Explorer right-click your workbook and choose Insert > Module. All the code below goes in that module.
  2. Decide what you are waiting for. There are two common cases: a fixed amount of time (for example, let a web query or external refresh finish) or a condition (for example, a cell equals "Done"). Use Application.Wait for the first and a polling loop for the second.
  3. Fixed pause with Application.Wait. This line pauses the macro for five seconds:
    Application.Wait Now + TimeValue("00:00:05")
    The argument is an absolute time, so Now + TimeValue(...) means "five seconds from now". Excel remains unresponsive during this pause; it does not process clicks or keystrokes.
  4. Poll a condition without freezing Excel. Use a Do loop with DoEvents so Excel can repaint and respond:
    Dim startTime As Double
    startTime = Timer
    Do While Range("A1").Value <> "Done"
        DoEvents
        If Timer - startTime > 30 Then
            MsgBox "Timed out waiting for A1"
            Exit Do
        End If
    Loop
    This waits until A1 says "Done", but gives up after 30 seconds so the macro cannot hang forever. Timer returns seconds since midnight, so this works for waits under 24 hours.
  5. Add a short delay inside the loop. Polling thousands of times per second wastes CPU. Add a small pause each pass:
    Application.Wait Now + TimeValue("00:00:01")
    Place it inside the loop after DoEvents. One-second granularity is enough for most spreadsheet tasks.
  6. Use Sleep for millisecond precision (Windows only). At the very top of the module, above 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 Sleep 500 to pause half a second. Like Application.Wait, Sleep blocks Excel, so combine it with DoEvents if the UI must stay responsive.
  7. Test with a message box. Run the macro with F5. If it finishes instantly, your condition was already true; if it never finishes, your condition never becomes true. Check the cell reference and the exact text being compared (extra spaces and case can matter).
  8. Stop a runaway loop. Press Esc or Ctrl+Break (Windows) or Cmd+Period (Mac). If that does not respond, close Excel from Task Manager (Windows) or Force Quit (Mac); unsaved changes will be lost.

Common Mistakes

  • Forgetting DoEvents. A tight Do While loop without it makes Excel look frozen and can block the very refresh or calculation you are waiting for.
  • No timeout. A loop that waits for a condition that never occurs runs forever. Always include a time limit and an Exit Do.
  • Comparing the wrong thing. Range("A1").Value may be a number, a date or text with trailing spaces. Use Trim and compare explicitly, or check IsEmpty.
  • Using Wait for long pauses. Application.Wait only accepts whole seconds and blocks the UI. For anything over a few seconds, a polling loop with DoEvents is friendlier.
  • Assuming Sleep works on Mac. The Windows API declaration fails on Mac. Use Application.Wait there.
  • Waiting on a background refresh that never starts. If you are waiting for a query or external data, make sure the refresh was actually triggered before the loop begins.

Tips

  • Put the timeout value in a constant at the top of the procedure so it is easy to adjust.
  • Use Application.ScreenUpdating = False before a long loop and set it back to True at the end to speed things up, but only if the user does not need to watch the sheet.
  • For waits tied to calculation, Application.Calculate followed by a check of Application.CalculationState is more reliable than a blind pause.
  • Keep polling intervals at one second or more unless you have a specific reason for finer timing.
  • Comment the condition you are waiting for; six months later it will not be obvious.

Troubleshooting

  • Macro never ends: press Esc or Ctrl+Break. Then add a timeout and a message box that prints the current value of the cell you are checking, so you can see why the condition never matches.
  • Excel freezes during the wait: you are using Application.Wait or Sleep without DoEvents. Switch to a Do loop with DoEvents.
  • Compile error on the Declare line: you are on a 64-bit Office install without PtrSafe, or on Mac. Use the #If VBA7 block shown above, or drop Sleep and use Application.Wait.
  • Loop exits immediately: the condition is already true at the start, or you used Do Until where Do While was intended. Check the logic direction.
  • "Sub or Function not defined" for Sleep: the Declare statement is missing or is inside a procedure instead of at the top of the module.

Frequently Asked Questions

What is the difference between Application.Wait and Sleep?

Application.Wait pauses until a given time and works on Windows and Mac. Sleep pauses for a number of milliseconds, is Windows-only, and needs a Declare statement. Both block Excel; add DoEvents in a loop if the UI must stay responsive.

Can I wait for a cell to change without freezing Excel?

Yes. Use a Do While loop that checks the cell and calls DoEvents each pass, with a timeout so it cannot run forever. Note that VBA cannot detect a manual edit while the macro is running; this pattern is for changes made by other code, links or refreshes.

Why does my wait loop run forever?

The condition you are checking never becomes true. Common causes are a wrong cell reference, trailing spaces in the compared text, or a refresh that was never triggered. Add a timeout and a debug message showing the current value.

How do I stop a running VBA loop?

Press Esc or Ctrl+Break on Windows, or Cmd+Period on Mac. If Excel is unresponsive, close it from Task Manager or Force Quit; any unsaved work will be lost.

Does DoEvents slow down my macro?

It adds a small overhead per pass, but it keeps Excel responsive and lets pending events process. Combined with a one-second pause inside the loop, the cost is negligible for typical spreadsheet tasks.

Can I use these loops on Excel for Mac?

Yes for Application.Wait and DoEvents. The Windows Sleep declaration does not work on Mac, so use Application.Wait there.