Complete the code to load data into an Excel worksheet.
QueryTable.Refresh BackgroundQuery=[1]Setting BackgroundQuery to False ensures the query runs synchronously and loads data directly into the worksheet.
Complete the code to load data into the Excel data model using Power Query.
WorkbookConnection.ModelTable.LoadToDataModel = [1]Setting LoadToDataModel to True loads the query results into the Excel data model.
Fix the error in the code to correctly load data into the worksheet.
ActiveWorkbook.Queries.Add Name:="SalesData", Formula:= "let Source = Excel.CurrentWorkbook(){[Name=\"Table1\"]}[Content] in Source" ActiveWorkbook.Connections.Add2 Name:="SalesData", _ Description:="", _ ConnectionString:= "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$", _ CommandText:= Array("SELECT * FROM [SalesData]"), _ lCmdtype:= [1]
The CommandType should be xlCmdText when using SQL command text in the connection.
Fill both blanks to load data into worksheet and refresh it.
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Range([1]), _ Destination:=Range("A1")) .QueryTable.Refresh BackgroundQuery:= [2] End With
The Source must be a valid range reference like "Sheet1!Table1" and BackgroundQuery should be False to refresh synchronously.
Fill all three blanks to load Power Query data into the data model and refresh it.
Dim cn As WorkbookConnection Set cn = ActiveWorkbook.Connections.Add2(Name:= "PQConnection", _ Description:= "Power Query Connection", _ ConnectionString:= "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$", _ CommandText:= Array("let Source = Excel.CurrentWorkbook(){[Name=\"Table1\"]}[Content] in Source"), _ lCmdtype:= [1]) cn.ModelTable.LoadToDataModel = [2] cn.Refresh [3]
Use xlCmdText for SQL command, set LoadToDataModel True to load into data model, and Refresh with BackgroundQuery False for synchronous refresh.