How to Fix Excel VBA RefreshAll Not Finishing Before Macro Continues
Question details
The VBA macro proceeds to subsequent commands before the RefreshAll operation finishes updating external SQL queries, causing the code to copy or process outdated data.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating workbook updates from SQL databases or external connections using VBA, and then attempting to copy the newly calculated results immediately after.
- Observed behavior
- The RefreshAll command runs asynchronously. Stepping through the macro line-by-line yields the correct output, but running the macro normally copies the data before the refresh and recalculation are actually complete.
Ensure you save a backup copy of your workbook and know the exact names of your external data connections before adjusting their background refresh properties.
Disable Background Refresh for Data Connections
Turning off background refresh forces Excel and VBA to wait until the external data query is completely finished before executing the next line of code.
By default, Excel allows external queries to refresh in the background so you can continue working. However, this asynchronous behavior causes VBA macros to run ahead of the data retrieval process.
Navigate to the 'Data' tab on the Excel ribbon and click on 'Queries & Connections' to open the side pane.
Right-click the specific data connection your macro relies on and select 'Properties' from the context menu.
In the 'Usage' tab of the Connection Properties dialog box, uncheck the box labeled 'Enable background refresh'.
Click 'OK' to save your changes, then run your VBA macro again to verify that it now waits for the data to populate.

Turn Off Background Query within VBA Code
Modify your VBA script to explicitly disable the background query parameter when executing the refresh command.
Use ADO for Direct SQL Lookups in VBA
Bypass standard Excel data connections by using ActiveX Data Objects (ADO) to run the SQL lookup synchronously within the macro itself.
Experience Lightweight and Reliable Spreadsheet Management with WPS Office
If complex Excel external connections and asynchronous VBA issues are slowing down your productivity, consider trying WPS Office. It provides a highly compatible, fast, and familiar spreadsheet environment that perfectly handles everyday data tasks without the heavy overhead.
- 1. Download WPS Office: Navigate to the official WPS website and download the free installation package for your operating system.
- 2. Install the Software: Follow the on-screen instructions to complete the lightweight installation in just a few minutes.
- 3. Open Your Workbooks: Launch WPS Spreadsheets and open your existing files to enjoy seamless format compatibility.

Frequently Asked Questions
Why does my VBA macro work correctly when stepping through it but fails when running normally?
When you step through the macro manually (using F8), you inadvertently give Excel enough time to complete the background data refresh before moving to the next line. When running normally, the VBA code executes instantaneously, outpacing the asynchronous SQL data retrieval.
What does 'Enable background refresh' actually do in Excel?
Background refresh allows external data connections to query the database in the background, keeping the Excel interface responsive so you can continue clicking and typing. However, it also allows VBA scripts to proceed without waiting for the new data to arrive.
Can I just use Application.Wait to delay my macro?
Using Application.Wait or Sleep commands is generally not recommended as a primary fix. Network speeds and SQL query times vary; a static wait time might be too short on a slow network or unnecessarily long on a fast one. Disabling background refresh is a more reliable, dynamic solution.
Does splitting the macro into two parts solve the asynchronous issue?
Splitting the macro—one to trigger the RefreshAll and another to copy the data—can act as a temporary workaround. However, it requires the user to manually trigger the second macro only after observing that the data refresh has completed, which defeats the purpose of full automation.




