VBA & Macro Problems

How to Fix Excel VBA External Data Refresh Failing on Workbook Startup

Elise WilliamsElise Williams Oct 23, 2026 870 views

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.

How to Fix Excel External Data Refresh on Workbook Startup
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.
Before you start

Ensure you have macro execution enabled in your Trust Center settings and that your external data server is currently online and accessible.

Solution 1Recommended

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.

1
Access the VBA Editor

Press the 'Alt + F11' shortcut on your keyboard while the spreadsheet is open to launch the Visual Basic for Applications (VBA) editor.

2
Locate the Startup Script

In the Project Explorer pane on the left side, double-click on 'ThisWorkbook' to open the code window containing your workbook-level events.

3
Implement DoEvents and Delay

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".

4
Save and Test

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.

Add a Delay and DoEvents to the Workbook_Open Macro
Testing Best Practices: Always test the startup macro from various entry points: opening from within the app, opening from a taskbar shortcut, and double-clicking the file in a directory to ensure consistent behavior.
Advanced Spreadsheet Data Management

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. 1. Install WPS Office: Download and install the free WPS Office suite to access the powerful Spreadsheet application.
  2. 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the external data connections.
  3. 3. Enable Macro Execution: Click 'Enable Macros' on the security warning bar that appears to allow your automated refresh scripts to run safely.
  4. 4. Manage Data and Code: Navigate to the 'Developer' tab to open the VBA editor and optimize your startup macros with ease.
High compatibility with Microsoft Excel macro-enabled formats (.xlsm)Built-in Visual Basic editor to seamlessly run and edit VBA scriptsLightweight architecture ensuring faster startup and application loadingAdvanced tools to manage external server and database connections
microsoft office alternative - wps office

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.