QueryTables.Add 方法 (Excel)

建立新的查詢資料表。

語法

運算式新增 (機、 目的地Sql)

表達 代表 QueryTables 物件的 變數。

參數

名稱 必要/選用 資料類型 描述
Connection 必要 Variant 查詢表的資料來源。 可以是下列其中一項:
  • 包含 OLE DB 或 ODBC 連接字串的字串。ODBC 連接字串具有 "ODBC;<連接字串>" 的格式。 ODBC 連接字串的格式 ODBC;<connection string> 為 。
  • A QueryTable object from which the query information is initially copied, including the connection string and the SQL text, but not including the Destination range. Specifying a QueryTable object causes the Sql argument to be ignored.
  • ADO 或 DAO Recordset 物件。 資料是從 ADO 或 DAO 資料錄集讀取而來。 Microsoft Excel 會保留資料錄集,一直到刪除查詢表或變更連接為止。 產生的查詢表無法編輯。
  • Web 查詢。 格式為 的字串, URL;<url> 其中 URL; 為必要但未當地語系化,而字串的其餘部分則用於 Web 查詢的 URL。
  • Data Finder. 格式 FINDER;<data finder file path> 為 的字串,其中 FINDER; 為必要但未當地語系化。 The rest of the string is the path and file name of a Data Finder file (*.dqy or *.iqy). 執行 Add 方法時會讀取檔案;後續呼叫查詢資料表的 Connection 屬性會傳回開頭 ODBC; 為 或 URL; 的字串。
  • 文字檔。 格式 TEXT;<text file path and name> 為 的字串,其中 TEXT; 為必要但未當地語系化。
目的地 必要 Range 位於查詢表目的範圍 (即將要放置所產生查詢表的範圍) 左上角的儲存格。 目的範圍必須在包含運算式所指定之 QueryTables 物件的工作表上。
Sql 選用 Variant 要在 ODBC 資料來源上執行的 SQL 查詢字串。 當您使用 ODBC 資料來源時,這個引數是選擇性的 (如果您未在此處指定,您應該先使用查詢表的 Sql 屬性加以設定,然後再重新整理資料表) 。 將 QueryTable 物件、文字檔、ADO 或 DAO Recordset 物件指定為資料來源時,您會無法使用此引數。

傳回值

代表新查詢 表的 QueryTable 物件。

註解

在呼叫 Refresh 方法之前,不會執行這個方法所建立的查詢。

範例

此範例會建立以 ADO 記錄集為基礎的查詢資料表。 基於回溯相容性,這個範例將會保留現有的欄排序和篩選設定,以及版面配置資訊。

Dim cnnConnect As ADODB.Connection 
Dim rstRecordset As ADODB.Recordset 
 
Set cnnConnect = New ADODB.Connection 
cnnConnect.Open "Provider=SQLOLEDB;" & _ 
    "Data Source=srvdata;" & _ 
    "User ID=testac;Password=4me2no;" 
 
Set rstRecordset = New ADODB.Recordset 
rstRecordset.Open _ 
    Source:="Select Name, Quantity, Price From Products", _ 
    ActiveConnection:=cnnConnect, _ 
    CursorType:=adOpenDynamic, _ 
    LockType:=adLockReadOnly, _ 
    Options:=adCmdText 
 
With ActiveSheet.QueryTables.Add( _ 
        Connection:=rstRecordset, _ 
        Destination:=Range("A1")) 
    .Name = "Contact List" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .BackgroundQuery = True 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = True 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .PreserveColumnInfo = True 
    .Refresh BackgroundQuery:=False 
End With

這個範例會將固定寬度的文字檔匯入到新的查詢表中。 該文字檔的第一欄為五個字元寬度,而且是以文字形式匯入。 第二欄為四個字元寬度,會予以略過。 該文字檔的其餘部分則會匯入到第三欄,並且會套用通用格式。

Set shFirstQtr = Workbooks(1).Worksheets(1) 
Set qtQtrResults = shFirstQtr.QueryTables.Add( _ 
    Connection := "TEXT;C:\My Documents\19980331.txt", 
    Destination := shFirstQtr.Cells(1,1)) 
With qtQtrResults 
    .TextFileParsingType = xlFixedWidth 
    .TextFileFixedColumnWidths := Array(5,4) 
    .TextFileColumnDataTypes := _ 
        Array(xlTextFormat, xlSkipColumn, xlGeneralFormat) 
    .Refresh 
End With

這個範例會在現用工作表上建立新的查詢表。

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

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應