Where to Use DoEvents in Excel VBA to Wait for Operations
Question details
Find out where to use DoEvents in Excel VBA and how to wait for an operation to finish.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A VBA macro needs to continue after a calculation or data refresh completes.
- Observed behavior
- Ordinary VBA statements run sequentially, but some operations can continue in the background and need an operation-specific completion check.
Identify the operation your macro needs to wait for, such as calculation or a QueryTable refresh. In Excel, open the VBA editor with Alt+F11 and locate the macro that starts the operation.
Wait for Excel calculation with a controlled DoEvents loop
Use this approach when the macro must wait until Excel reports that calculation is complete.
DoEvents gives Excel and the operating system an opportunity to process pending events; it does not itself guarantee that an operation has finished. Application.CalculationState applies to calculation, not to every kind of background operation.
In Excel, press Alt+F11, open the relevant module, and find the line that starts or depends on calculation.
After starting calculation, add: Dim deadline As Date: deadline = DateAdd("s", 30, Now) Do While Application.CalculationState <> xlDone And Now < deadline DoEvents Loop If Application.CalculationState <> xlDone Then MsgBox "Calculation did not finish within 30 seconds." Exit Sub End If
Place the statements that depend on the calculated results after the loop, then run the macro from the VBA editor with F5.

Run a supported QueryTable refresh synchronously
Use this when a QueryTable supports synchronous refresh and the macro needs its data before continuing.
Work with spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format support for easier migration. It does not fix Excel VBA behavior; check macro support for your WPS edition before relying on an existing VBA workbook.

Frequently Asked Questions
Do I need DoEvents between ordinary VBA statements?
No. VBA normally executes ordinary statements sequentially. Use DoEvents when you need to let Excel or the operating system process events, not to force normal lines to run in order.
Where should I put DoEvents in a wait loop?
Put DoEvents inside a controlled loop that checks the relevant completion state, such as Application.CalculationState <> xlDone. Include a timeout so the macro does not wait indefinitely.
Does xlDone mean my data refresh has finished?
Not necessarily. Application.CalculationState reports calculation status, not the completion of every background refresh. Check the refresh object's own settings, state, or completion event.
Can DoEvents cause a macro to run twice?
It can allow user actions or events to run while the macro is paused. If those actions can start the same procedure again, add a guard and protect any shared state.




