Skip to main content

QueryTables object

Table of contents
  1. Remarks
  2. Example

A collection of QueryTable objects.

Remarks

Each QueryTable object represents a worksheet table built from data returned from an external data source.

Example

Use the QueryTables property of the Worksheet object to return the QueryTables collection.

The following example displays the number of query tables on the active worksheet.

MsgBox ActiveSheet.QueryTables.Count

Use the Add method to create a new query table and add it to the QueryTables collection. The following example creates a new query table.

Dim qt As QueryTable
sqlstring = "select 96Sales.totals from 96Sales where profit < 5"
connstring = "ODBC;DSN=96SalesData;UID=Rep21;PWD=NUyHwYQI;Database=96Sales"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("B1"), Sql:=sqlstring)
    .Refresh
End With

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>