How to Prevent Background Data Model Refreshes in Excel VBA
Question details
The user needs to prevent Excel's Data Model from running background refreshes asynchronously when VBA copies CubeValue formulas to new worksheets, as standard UI settings do not fully stop this behavior.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Executing a VBA macro that duplicates worksheets containing CubeValue formulas linked to a large Data Model, causing asynchronous recalculations and queries.
- Observed behavior
- CubeValue formulas fail to return immediate final results because Data Model refreshes and workbook calculations continue asynchronously in the background, ignoring basic refresh configurations.
Before modifying your macro code, ensure your workbook is saved and create a backup copy to prevent accidental data corruption during asynchronous refresh loops.
Control Calculation State and Use a Wait Loop in VBA
Manually suspend workbook calculations and use VBA to monitor the specific connection's refresh status, ensuring operations complete before proceeding.
Because Data Models and Cube functions process queries asynchronously, turning off 'Enable Background Refresh' in the Excel user interface is not always sufficient. You must explicitly control the calculation state and use a wait-and-check loop in your code to verify that the connection has finished refreshing before allowing Excel to evaluate the formulas.
Open the VBA Editor (ALT + F11), locate your macro, and insert `Application.Calculation = xlCalculationManual` at the beginning of the script to prevent immediate formula evaluation.
Copy your formulas or worksheets as intended within the macro, then execute a refresh on the specific Data Model connection using `ActiveWorkbook.Connections("YourConnectionName").Refresh`.
Write a `Do While` loop that checks `ActiveWorkbook.Connections("YourConnectionName").OLEDBConnection.Refreshing`. Inside the loop, use `Application.Wait` to pause macro execution momentarily until the property returns False.
Once the loop completes (indicating the refresh is finished), insert `Application.Calculation = xlCalculationAutomatic` and `Application.Calculate` to safely evaluate the CubeValue formulas.

Programmatically Disable BackgroundQuery for OLEDB Connections
Use a VBA script to force all workbook connections to refresh synchronously by disabling their BackgroundQuery properties before running your main macro.
Looking for a fast, lightweight alternative for your spreadsheet tasks?
If complex Data Models, asynchronous calculation bugs, and heavy VBA processing in Microsoft Excel are causing bottlenecks, try WPS Office. It provides a lightweight, highly compatible spreadsheet environment that executes standard VBA macros quickly without the heavy overhead of Microsoft's advanced BI tools.

Frequently Asked Questions
Why do my CubeValue formulas display '#GETTING_DATA' during a VBA macro?
CubeValue formulas retrieve data from the Excel Data Model asynchronously. If a VBA macro copies formulas and finishes execution before the Data Model completes its background query, Excel displays '#GETTING_DATA' until the calculation is finalized.
Does disabling 'Enable Background Refresh' in the UI stop all background queries?
No. Disabling that option primarily stops scheduled or UI-driven background refreshes. Certain VBA operations and formula evaluations tied to the Data Model may still initiate asynchronous data retrieval processes.
Can I use DoEvents in VBA to wait for the Data Model refresh?
While DoEvents yields execution control to the operating system temporarily, it does not reliably halt VBA execution until an OLEDB connection refresh finishes. A loop that explicitly checks the connection's 'Refreshing' property is highly recommended instead.




