Sub ModifyPowerQuery(filePath As String)
Dim wb As Workbook
Set wb = ThisWorkbook
Dim qry As QueryTable
Set qry = wb.Queries("My Query") ' Modify the existing "My Query" power query
' Set the new command text for the query
qry.CommandText = "SELECT * FROM """ & filePath & """"
Dim ws As Worksheet
Set ws = wb.Worksheets("Sheet1")
Dim qt As QueryTable
Set qt = ws.QueryTables("My Query") ' Modify the existing query table on the worksheet
qt.Refresh ' Refresh the query to load the data from the new file
End Sub
1条答案
按热度按时间r3i60tvu1#
你可以写一个子函数,它接受你的参数并为你重写一个查询。下面是一个例子来说明我的意思。