在分配表数据源时将变量设置为查询名称

时间:2016-08-08 21:52:10

标签: excel-vba vba excel

我需要语法帮助来传递查询名称,并从工作簿中现有的查询列表中设置表的数据源。

Sub Button15_Click()

Dim qName As WorkbookQuery
Dim nSht As Worksheet

qName = Sheets("MainForm").Range("J3").Value
 WkBookName = qName
 nSht = Sheets("InvoiceForm").Select
Debug.Print WkBookName
LoadToWorksheetOnly qName, nSht

end sub

Sub LoadToWorksheetOnly(query As WorkbookQuery, currentSheet As Worksheet)
' The usual VBA code to create ListObject with a Query Table
' The interface is not new, but looks how simple is the conneciton string of Power Query:
' "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & query.Name

With currentSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & query.Name _
    , Destination:=Range("$A$18")).QueryTable
    .CommandType = xlCmdDefault
    .CommandText = Array("SELECT * FROM [" & query.Name & "]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = False
    .Refresh BackgroundQuery:=False
End With

End Sub

不喜欢qName变量语法。错误消息_对象变量或未设置块变量。关于qName的设置。

0 个答案:

没有答案