VBA to refresh PowerQuery tables

stuartmacdonald

New Member
Joined
May 26, 2009
Messages
48
Help!!

I'm trying to use PowerQuery with PowerApps, however, you cannot have a PowerApps connection to a PowerQuery table, its stops refreshing the table once connected to PowerApps, however the PowerQuery itself still updates. As you also cannot use a macro-enabled workbook with a connection to PowerApps, I'm trying to develop a macro in a separate workbook that opens up the 'Project check.xlsm' workbook, deletes the PowerQuery tables (x5), reloads them and closes the workbook. This way I can manually refresh with one click....

However, I have no experience of VBA... Below is what I have come up with by inserting an instruction to open the workbook above a macro to delete and reload tables. But it's not working... doesn't even get into the macro - stops at Windows("Project check.xlsx").Activate

Any ideas (or alternative suggestions) very welcome....

Code:
Sub Open_Workbook_Basic()    
    Workbooks.Open Filename:="C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Project check.xlsx"
    
Sub Refresh_tables()
'
' Refresh_tables Macro
'
    Windows("Project check.xlsx").Activate
    Cells.Select
    Range("CLES_days_spent[[#Headers],[Contract_No&Desc]]").Activate
    Selection.ClearContents
    ActiveWorkbook.Connections("Query - CLES_days_spent").Refresh
    With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
        Connections("Query - CLES_days_spent"), Destination:=Range("$L$1")). _
        TableObject
        .RowNumbers = False
        .PreserveFormatting = True
        .RefreshStyle = 1
        .AdjustColumnWidth = True
        .ListObject.DisplayName = "CLES_days_spent"
        .Refresh
    End With
    Sheets("CERN").Select
    Cells.Select
    Range("CERN_days_spent[[#Headers],[Contract_No&Desc]]").Activate
    Selection.ClearContents
    ActiveWorkbook.Connections("Query - CERN_days_spent").Refresh
    With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
        Connections("Query - CERN_days_spent"), Destination:=Range("$B$1")). _
        TableObject
        .RowNumbers = False
        .PreserveFormatting = True
        .RefreshStyle = 1
        .AdjustColumnWidth = True
        .ListObject.DisplayName = "CERN_days_spent"
        .Refresh
    End With
    Sheets("Grant").Select
    Cells.Select
    Range("Grant_days_spent[[#Headers],[Contract_No&Desc]]").Activate
    Selection.ClearContents
    ActiveWorkbook.Connections("Query - Grant_days_spent").Refresh
    With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
        Connections("Query - Grant_days_spent"), Destination:=Range("$L$1")). _
        TableObject
        .RowNumbers = False
        .PreserveFormatting = True
        .RefreshStyle = 1
        .AdjustColumnWidth = True
        .ListObject.DisplayName = "Grant_days_spent"
        .Refresh
    End With
    Sheets("Training_events").Select
    Cells.Select
    Selection.ClearContents
    ActiveWorkbook.Connections("Query - Training_events_days_spent").Refresh
    With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
        Connections("Query - Training_events_days_spent"), Destination:=Range("$A$1") _
        ).TableObject
        .RowNumbers = False
        .PreserveFormatting = True
        .RefreshStyle = 1
        .AdjustColumnWidth = True
        .ListObject.DisplayName = "Training_events_days_spent"
        .Refresh
    End With
    Sheets("Membership").Select
    Cells.Select
    Selection.ClearContents
    ActiveWorkbook.Connections("Query - Membership_days_spent").Refresh
    With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
        Connections("Query - Membership_days_spent"), Destination:=Range("$A$1")). _
        TableObject
        .RowNumbers = False
        .PreserveFormatting = True
        .RefreshStyle = 1
        .AdjustColumnWidth = True
        .ListObject.DisplayName = "Membership_days_spent"
        .Refresh
    End With
    ActiveWorkbook.Save
    ActiveWindow.Close
End Sub
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top