How to edit the source of a power query using VBA?

独自空忆成欢 提交于 2019-12-01 22:40:33

You can use ActiveWorkbook.Queries.Item to get the query you want and use the Formula property to update the query's formula, like so:

ActiveWorkbook.Queries.Item("LATEST").Formula = "let MyNewFormula = 1 + 1 in Source"

Note: this only works on Excel 2016 or later.

I beleive you'd better avoid such methods as they can cause compatibility problems as well as some other.

If you learn M, you probably won't need to edit code with VBA.

A bit late to the party, but additionally, for anyone who views this moving forward one can use:

Thisworkbook.Queries("LATEST").Formula = mFormula

Note, ThisWorkbook is preferable to ActiveWorkbook.

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