How to Speed Up Excel VBA Queries to Azure SQL
Question details
Optimize an Excel VBA macro executing multiple parameterized queries to Azure SQL to reduce processing time.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running hundreds of row-by-row database queries from an Excel VBA macro to an Azure SQL instance.
- Observed behavior
- Processing queries individually takes excessive time due to repeated network round trips, connection overhead, and individual command executions, despite each query being fast in SSMS.
Ensure you have the Microsoft ActiveX Data Objects (ADODB) library enabled in your VBA editor references, and confirm your network connection to the Azure SQL server is stable.
Use Set-Based Operations Instead of Row-by-Row Queries
Combine multiple row queries into a single operation using a staging table or table-valued parameter to drastically reduce network latency.
Running hundreds of individual queries requires Excel to open and close multiple network packets, handle command executions, and parse results repeatedly. This approach is highly inefficient over a network connection to a cloud database like Azure SQL.
Instead of looping through rows in VBA, upload all necessary query parameters to a temporary table on the SQL server in one batch, then execute a single JOIN query to return all results at once.
Consolidate the parameter values from your 517 rows into a single continuous range or an array within your VBA script.
Write a VBA script to create a temporary staging table in Azure SQL and execute a bulk insert or a consolidated INSERT statement to push all Excel values in one network trip.
Run a single SELECT query that joins your main Azure SQL tables with the staging table, returning the entire dataset back to Excel.
Use the ADODB Recordset's CopyFromRecordset method to write the compiled data directly into your Excel worksheet in one block, minimizing Excel I/O operations.

Optimize the ADODB Connection and Command Reusability
If individual query calls are unavoidable, reuse a single open database connection and bind parameters to a prepared command object.
Run Optimized VBA Macros Seamlessly in WPS Office
WPS Spreadsheet features robust built-in support for VBA and ADODB components, enabling you to build, debug, and run high-performance queries against cloud databases like Azure SQL smoothly.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet containing the database logic.
- 2. Access the VBA Editor: Navigate to the Developer tab and click on 'Visual Basic', or simply press Alt + F11 to open the code editor.
- 3. Enable ADODB References: In the VBA editor, click Tools > References, and check 'Microsoft ActiveX Data Objects Library' to ensure database connectivity works.
- 4. Run the Macro: Execute your optimized query script. WPS Office will process the ADODB connection and efficiently render the returned SQL records in your sheet.

Frequently Asked Questions
Why does my SQL query run fast in SSMS but slow in Excel VBA?
SQL Server Management Studio (SSMS) runs directly on the database context and fetches data natively. In contrast, Excel VBA running queries row-by-row suffers from network latency, protocol overhead, connection setup time, and the time required to read/write individual worksheet cells for every iteration.
How do I measure the execution time of my VBA macro?
You can use the built-in VBA 'Timer' function. Store the start time in a variable before the ADODB execute command (e.g., StartTime = Timer), and calculate the difference after the query completes (e.g., MsgBox "Time taken: " & Timer - StartTime & " seconds").
What is a Table-Valued Parameter (TVP) in SQL?
A Table-Valued Parameter allows client applications, such as an Excel macro via ADODB, to pass multiple rows of data to a SQL Server stored procedure or function in a single network round trip, vastly improving performance over individual insert or select statements.
Does WPS Office support ADODB connections to Azure SQL?
Yes, WPS Office fully supports standard VBA modules and COM object references, including Microsoft ActiveX Data Objects (ADODB). You can connect to external databases like Azure SQL just as you would in Microsoft Excel.




