When I’ve finished building a solution in Excel, I like to give it a little polish, and really make it easy for my users to update it. The last thing I want to do is to send them right clicking and refreshing everywhere, so I program a button to refresh Power Query with VBA.
The interesting part about the above statement is that Power Query doesn’t have any VBA object model, so what kind of black magic trick do we need to leverage to pull that off? As it turns out, it’s very simple… almost too simple in fact.
A Simple Query
- Click in the blue table
- Go to Power Query –> From Table
- Let’s sort Animal ascending (just so we know something happened)
- Next save and Exit the query
At this point, we should get a new “Sheet2” worksheet, with our table on it:
The Required VBA Code
Next, we need to build our VBA for refreshing the table. Rather than record and tweak a macro, I’m just going to give you the code that will update all Query Tables in the entire workbook in one shot. But to use it, you need to know the secret handshake:
- Press Alt + F11
This will open the Visual Basic editor for you. If you don’t see a folder tree at the left, then press CTRL+R to make it show up.
- Find your project in the list (It should be called “"VBA Project (Selecting Data.xlsx)”
- Right click that name and choose “Insert Module”
- In the window that pops up, paste in the following code:
Public Sub UpdatePowerQueries()
' Macro to update my Power Query script(s)
Dim lTest As Long, cn As WorkbookConnection
On Error Resume Next
For Each cn In ThisWorkbook.Connections
lTest = InStr(1, cn.OLEDBConnection.Connection, "Provider=Microsoft.Mashup.OleDb.1", vbTextCompare)
If Err.Number <> 0 Then
If lTest > 0 Then cn.Refresh
NOTE: The code above was updated 2015-08-19 to reflect the method posted here: http://www.excelguru.ca/blog/2015/08/19/update-refresh-power-queries-with-vba/
Now, I’ll admit that I find this a little looser than I generally like. By default, all Power Query scripts create a new connection with the name “Power Query –“ the name of your query. I’d prefer to check the type of query, but this will work.
Speaking of working, let’s prove it… But first, close the Visual Basic Editor.
Proving The Refresh Works
The easiest way to do this is to go back to the table on Sheet 1 and add a new row to the table. I’m going to do that first, then I’m going to:
- Press Alt + F8
- Choose “UpdatePowerQueries”
- Click Run
- Go back to Sheet2 to verify it’s updated
If all goes well, you should now have another row of data in your table, as I do:
Let’s face it, that’s probably harder than going to Data –> Refresh All. The goal here was to make it easier for my users. So let’s do that now.
- Return to Sheet 1
- Go to the Developer Tab (if you don’t see it, right click the ribbon, choose “Customize Ribbon” and check the box next to the Developer tab to expose it)
- Click Insert and select the button in the top left
- Left click and drag a button onto your worksheet
When you let go, you’ll be prompted to assign a macro.
- Choose “UpdatePowerQueries” and click OK
- While the dots are still on the corners, click in the text
- Backspace it out and replace it with something helpful like “Update Queries” (if you click elsewhere, you’ll need to right click the button to get the selection handles back.)
- Click in the worksheet to de-select the button
That’s it. Test it again by adding some more data to the table then clicking the button.
I write a lot of VBA to make my users lives easier, and generally use this kind of technique as part of a bigger goal. But regardless, it can still be useful as a stand alone routine if you want to avoid having to train users on how to do things through the ribbon.