VBA & Macro Problems

Where to Use DoEvents in Excel VBA to Wait for Operations

Elise WilliamsElise Williams Nov 18, 2026 871 views

Question details

Find out where to use DoEvents in Excel VBA and how to wait for an operation to finish.

Where to Use DoEvents in Excel VBA to Wait for Operations
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the macro

In Excel, press Alt+F11, open the relevant module, and find the line that starts or depends on calculation.

2
Add a bounded wait loop

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

3
Continue after the check

Place the statements that depend on the calculated results after the loop, then run the macro from the VBA editor with F5.

Wait for Excel calculation with a controlled DoEvents loop
Prevent re-entry: DoEvents can allow other events or user actions to run while the macro is waiting. Protect shared state or use a guard if the same procedure could be started again.
Free Microsoft Office alternative

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.

Open and edit spreadsheets with a familiar interfaceCompatible with Microsoft Excel XLS and XLSX formatsReview spreadsheet data and formulas without requiring ExcelMove everyday Office documents into a lightweight suite
microsoft office alternative - wps office

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.