【问题标题】:How to change only source and refresh of a power query using VBA?如何使用 VBA 仅更改电源查询的源和刷新?
【发布时间】:2021-04-30 06:45:21
【问题描述】:

我有这个问题我正在尝试更新 PowerQuery 中的连接(源)。 你能帮我在VBA中怎么做吗??

我的 DebugPrint(抱歉,我有一个波兰语版本的 excel365;)):

TEST_CHANGE let
    Źródło = Excel.Workbook(File.Contents("C:\Users\revol\Desktop\PQ_11.xlsm"), null, true),
    TEST_CHANGE_Sheet = Źródło{[Item="TEST_CHANGE",Kind="Sheet"]}[Data],
    #"Nagłówki o podwyższonym poziomie" = Table.PromoteHeaders(TEST_CHANGE_Sheet, [PromoteAllScalars=true]),
    #"Zmieniono typ" = Table.TransformColumnTypes(#"Nagłówki o podwyższonym poziomie",{{"Column1", type text}, {"Column2", type text}, {Column3", Int64.Type}, {"Column4", type text}, {"Fiscal year / period", type text}, {"Some else", type number}}) in
    #"Zmieniono typ"
-----------------------;

我尝试使用此指令,但它不起作用; (

ActiveWorkbook.Queries.Item("MyQuery").Formula = "[Excel.Workbook(File.Contents("C:\Users\revol\Desktop\change_source.xlsm"), null, true)]"

提前感谢您的帮助

【问题讨论】:

    标签: excel vba powerquery


    【解决方案1】:

    这个函数会刷新所有的powerquery连接:

    Sub Refresh_All_Data_Connections()
    Dim objConnection, bBackground
        For Each objConnection In ThisWorkbook.Connections
            'Get current background-refresh value
            bBackground = objConnection.OLEDBConnection.BackgroundQuery
    
            'Temporarily disable background-refresh
            objConnection.OLEDBConnection.BackgroundQuery = False
    
            'Refresh this connection
            objConnection.Refresh
    
            'Set background-refresh value back to original value
            objConnection.OLEDBConnection.BackgroundQuery = bBackground
        Next
    

    结束子

    【讨论】:

    • 我只是想将源更改为其他源并刷新它。我有源“C:\Users\revol\Desktop\PQ_11.xlsm”,我已经将它更改为“C:\Users\revol\Desktop\change_source.xlsm”
    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2022-06-17
    • 2019-04-21
    • 1970-01-01
    • 2020-05-26
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多