Name

querytables.Add(Connection, Destination, [Sql])

Synopsis

Creates a new query table and adds it to the worksheet. Returns a QueryTable object.

Argument

Description

Connection

A string or object reference identifying the source of the data.

Destination

A Range object identifying the upper-lefthand corner of the destination of the query table.

Sql

If the Connection argument is an ODBC data source, this argument is a string containing the SQL query to perform. Otherwise, including this argument either causes an error or is ignored.

The Connection argument has different forms, depending on the type of data source being queried as described in the following table:

Data source

Use to

Sample connection argument

Web page

Perform a web query

"URL;http://finance.yahoo.com/q/ecn?s=yhoo"

The Add method’s Connection argument uses the "URL;" prefix when performing a query on a web page. For example, the following code creates a new query table containing a real-time stock quote from Yahoo!:

    Set ws = ThisWorkbook.Sheets("Other QueryTables")
    strConn = "URL;http://finance.yahoo.com/q/ecn?s=dell"
    Set qt = ws.QueryTables.Add(strConn, [QueryDestination])
    qt.Refresh

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.