• Custom Search

How to view the query the SQL for an Excel worksheet with data source

 

Save the following code in a module.

Sub print_query_table_sql()
    Dim s As Worksheet
    Dim qt As QueryTable
    Dim r As Range
    Dim sql As String
    Set s = ActiveSheet
    Set qt = s.QueryTables(1)
    sql = qt.sql
    Set r = s.Cells(1).SpecialCells(xlLastCell).Offset(1, 1)
    r = sql
End Sub

 

Select your worksheet that contains data from a database source, then run the above macro.

It will print out the SQL in the last cell. (Ctrl-End).

Technorati tags: ,

Read more work articles

This entry was posted in work. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*