Use a Worksheet Cell as SQL Command Text in Excel 2016
Question details
Use one complete SQL query stored in a worksheet cell as the source for approximately two dozen Excel database connections, including queries that execute CREATE TABLE, INSERT, and SELECT statements consecutively.

- Product
- Microsoft Excel 2016
- Device & OS
- not provided
- Scenario
- Maintain the SQL text in one location so that multiple database queries can reuse it when refreshed.
- Observed behavior
- The existing database connection and query work, but the standard connection Command text box does not directly bind an entire SQL statement to a worksheet cell.
Confirm the database type, server name, database name, and permissions required by the SQL statements; the Power Query example below targets SQL Server. Test the batch in an approved test database, because CREATE TABLE and INSERT change database state and may run again on refresh.
Read a named worksheet cell with Power Query and Sql.Database
Use this approach for SQL Server when you want multiple Power Query queries to read the same SQL text from a worksheet cell.
The standard Excel connection Command text box is not a formula field: entering a cell reference there does not substitute the cell's contents. Power Query can explicitly read a named cell and pass its text to Sql.Database.
This creates Power Query queries rather than automatically changing your existing legacy connections. Each query can read the same named cell, but each refresh executes against its own configured database.
Enter the complete SQL text in one worksheet cell. Select that cell, click the Name Box to the left of the formula bar, type SqlText, and press Enter. Use a workbook-scoped name so other queries can find it.
Open Data > New Query > From Other Sources > Blank Query in Excel 2016. In builds with the newer ribbon labels, use Data > Get Data > From Other Sources > Blank Query.
In the Power Query Editor, select Home > Advanced Editor and replace the query with: let SqlText = Text.From(Excel.CurrentWorkbook(){[Name="SqlText"]}[Content]{0}[Column1]), Source = Sql.Database("YOUR_SERVER", "YOUR_DATABASE", [Query=SqlText]) in Source. Replace YOUR_SERVER and YOUR_DATABASE with your SQL Server connection details, then select Done.
When prompted, select the appropriate database authentication method and approve the native query only after reviewing its SQL. Set accurate privacy levels if requested, then choose Home > Close & Load to load the returned table into Excel.
In Data > Show Queries, right-click the query and choose Duplicate for each additional target. Open each duplicate's Advanced Editor and change its server or database as needed while keeping the SqlText cell reference. Edit the worksheet cell once, then use Data > Refresh All to rerun the queries.

Use VBA and ADODB for explicit SQL execution control
Use this approach when you need to manage database sessions, statement order, or multiple result sets more directly than Power Query allows.
Use WPS Spreadsheet to Review Exported SQL Results
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Excel format compatibility for an easy transition. Use WPS Spreadsheet to review and analyze exported database results; it does not resolve Excel 2016 connection settings or guarantee support for Power Query and ADODB workflows.

Frequently Asked Questions
Can I put a cell reference directly in Excel's connection Command text box?
Not as a built-in substitution for a complete SQL statement. Data > Connections > Properties > Definition shows literal command text, not a worksheet formula. Use Power Query to read a named cell or VBA to assign and execute its contents.
Can one cell supply SQL text to approximately two dozen connections?
Yes, if each query is configured to read that cell. In Power Query's Advanced Editor, use Excel.CurrentWorkbook() to access the workbook-scoped name SqlText, then pass the value to Sql.Database. Existing legacy connections must be adapted or replaced; naming the cell alone does not link them.
What delimiter should I use between CREATE TABLE, INSERT, and SELECT?
For SQL Server, use valid T-SQL statement syntax with semicolons where appropriate. Do not include GO in the text submitted through Sql.Database or ADODB: GO is interpreted by tools such as SQL Server Management Studio, not by the SQL Server engine. Other database providers may impose different multi-statement restrictions.
Why does a temporary table disappear before the SELECT runs?
A local SQL Server temporary table belongs to a session and execution scope. Creating it in one connection and selecting from it in another will not work. Submit the operations in one supported batch or use the same open ADODB connection for the commands.
Will changing the SQL cell immediately update every result table?
No. Change the cell, then use Data > Refresh All or refresh the relevant query individually. Each query reads the new text during its execution. Review data-changing statements first, since a refresh may repeat inserts or attempt to recreate tables.




