问题
I was searching for a simple vba code that open the prompt for change source for query that is xls sheet. I have a lot of query steps that i need to do every time that source is changing. So basically i would like to have macro that does this manual steps for a certain query name (i have a number of them in a workbook): in the query edit mode, click the source change button:source
macro should open the prompt so that i can manually pick the xls file:screen
also, the data can be in a sheet that have different name than previously, so i have to also navigate to proper sheet/table so that "navigation" prompt would be also nice to open so i can manually choose: button nagivation
回答1:
Create a cell in excel workbook that contains the full path of desired filename like c:\temp\directory\subdirectory\filename.xlsx and give that cell a range name, here "TheFilename" in my example
Create a cell in excel workbook that contains name of desired input tab on filename like Sheet1 and give that cell a range name, here "TheSheet" in my example
Use those range names in powerquery as variables using syntax
Excel.CurrentWorkbook(){[Name="rangeName"]}[Content]{0}[Column1]
as in this example
let
TheFilename = Excel.CurrentWorkbook(){[Name="TheFilename"]}[Content]{0}[Column1],
TheSheet= Excel.CurrentWorkbook(){[Name="TheSheet"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(TheFilename), null, true),
Data = Source{[Item=TheSheet,Kind="Sheet"]}[Data]
in Data
来源:https://stackoverflow.com/questions/60986680/vba-code-for-changing-query-xls-source-prompt