Excel VBA Wait Loop: Pause Code Until a Condition Is Met
In this article
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
Sleepfunction: aDeclarestatement at the top of a standard module. On Mac,Sleepis not available the same way; useApplication.Waitinstead.
Menu labels and the exact location of options can differ slightly by Excel version, platform and region.
Step-by-Step
- 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.
- 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.Waitfor the first and a polling loop for the second. - Fixed pause with Application.Wait. This line pauses the macro for five seconds:
The argument is an absolute time, soApplication.Wait Now + TimeValue("00:00:05")Now + TimeValue(...)means "five seconds from now". Excel remains unresponsive during this pause; it does not process clicks or keystrokes. - Poll a condition without freezing Excel. Use a
Doloop withDoEventsso Excel can repaint and respond:
This waits until A1 says "Done", but gives up after 30 seconds so the macro cannot hang forever.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 LoopTimerreturns seconds since midnight, so this works for waits under 24 hours. - Add a short delay inside the loop. Polling thousands of times per second wastes CPU. Add a small pause each pass:
Place it inside the loop afterApplication.Wait Now + TimeValue("00:00:01")DoEvents. One-second granularity is enough for most spreadsheet tasks. - Use Sleep for millisecond precision (Windows only). At the very top of the module, above any procedures, add:
Then call#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 IfSleep 500to pause half a second. LikeApplication.Wait,Sleepblocks Excel, so combine it withDoEventsif the UI must stay responsive. - 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).
- 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 tightDo Whileloop 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").Valuemay be a number, a date or text with trailing spaces. UseTrimand compare explicitly, or checkIsEmpty. - Using
Waitfor long pauses.Application.Waitonly accepts whole seconds and blocks the UI. For anything over a few seconds, a polling loop withDoEventsis friendlier. - Assuming
Sleepworks on Mac. The Windows API declaration fails on Mac. UseApplication.Waitthere. - 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 = Falsebefore a long loop and set it back toTrueat the end to speed things up, but only if the user does not need to watch the sheet. - For waits tied to calculation,
Application.Calculatefollowed by a check ofApplication.CalculationStateis 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.WaitorSleepwithoutDoEvents. Switch to aDoloop withDoEvents. - Compile error on the Declare line: you are on a 64-bit Office install without
PtrSafe, or on Mac. Use the#If VBA7block shown above, or dropSleepand useApplication.Wait. - Loop exits immediately: the condition is already true at the start, or you used
Do UntilwhereDo Whilewas intended. Check the logic direction. - "Sub or Function not defined" for Sleep: the
Declarestatement 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.