vba code for changing query xls source prompt

a 夏天 提交于 2021-02-11 14:13:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!