How to Fix Excel VBA External Data Refresh Failing on Workbook Startup
Question details
The user is experiencing an issue where an Excel VBA macro designed to refresh external data automatically upon workbook startup fails when the file is opened directly via a shortcut or double-click, though it works fine if opened from within an already active Excel session.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating external data connection refreshes using a Workbook_Open event macro upon launching a spreadsheet.
- Observed behavior
- The external data refresh executes prematurely before the necessary server connections are established when launching the application from a cold start, causing the macro to fail.
Ensure you have macro execution enabled in your Trust Center settings and that your external data server is currently online and accessible.
Add a Delay and DoEvents to the Workbook_Open Macro
Introducing a brief delay and using DoEvents allows the spreadsheet application enough time to establish network and server connections before executing the external data refresh command.
When opening a workbook via a shortcut or by double-clicking the file in Windows Explorer, the entire application undergoes a cold start. Background drivers and network connections take a moment to initialize.
If your Workbook_Open macro immediately triggers an external refresh, it may hit a timeout or connection error. Yielding execution back to the application momentarily ensures everything is fully loaded before the data query runs.
Press the 'Alt + F11' shortcut on your keyboard while the spreadsheet is open to launch the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left side, double-click on 'ThisWorkbook' to open the code window containing your workbook-level events.
Inside your Workbook_Open subroutine, add 'DoEvents' right before your refresh command to process pending background tasks. Alternatively, schedule the macro to run a few seconds later using: Application.OnTime Now + TimeValue("00:00:03"), "YourRefreshMacroName".
Save your VBA code and close the workbook. Completely exit the spreadsheet application, then double-click the workbook file from your desktop or folder to test if the data refreshes correctly on startup.

Easily Automate External Data Refreshes with WPS Spreadsheet
WPS Office offers a powerful spreadsheet application with robust support for VBA macros and external data queries, allowing you to streamline your workflows with faster load times.
- 1. Install WPS Office: Download and install the free WPS Office suite to access the powerful Spreadsheet application.
- 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the external data connections.
- 3. Enable Macro Execution: Click 'Enable Macros' on the security warning bar that appears to allow your automated refresh scripts to run safely.
- 4. Manage Data and Code: Navigate to the 'Developer' tab to open the VBA editor and optimize your startup macros with ease.

Frequently Asked Questions
Why does my data refresh macro work when the spreadsheet application is already open?
When the application is already running, foundational resources, add-ins, and network drivers are fully initialized. Launching a file from a shortcut triggers a cold start, meaning the application is still loading background components when the Workbook_Open macro fires.
What is the purpose of DoEvents in VBA?
DoEvents is a VBA command that temporarily pauses macro execution, yielding control back to the operating system. This allows the application to process background queued events, such as establishing server connections or updating the user interface, before returning to the macro script.
How long should I delay my startup macro for a data refresh?
A delay of 2 to 5 seconds is generally recommended. The exact time depends on your network speed and the complexity of the external data connection. Using Application.OnTime with a 3-second delay is a standard practice to ensure stability.
Will macros written for Microsoft Excel work in WPS Office?
Yes, WPS Spreadsheet provides strong compatibility for Excel VBA macros. As long as you have the appropriate VBA module enabled in WPS Office, you can view, edit, and execute your existing .xlsm startup scripts.




