Although not a precise solution, I identified a quirky work-around to display the error messages that Power Query would generally display, even when triggering a refresh from VBA. The caveat is that it will refresh all queries, but I propose a method to deal with that later on.
We all know that when you call the Workbook.RefreshAll
method from VBA, for some reason, when using it with Application.CalculateUntilAsyncQueriesDone
, all the Power Query Error messages get suppressed.
However, when you click on the 'Refresh All' button from the 'Data' tab of the Excel Ribbon, the error messages are displayed. Fortunately, you can trigger a ribbon command call from VBA using the CommandBars.ExecuteMso
method. So I tried to call the button, and it worked - the error messages were displayed.
You need the idMso
property of the 'Refresh All' button to trigger the command. It is "RefreshAll"
- no surprises there.
Here is a link to an post that helps you get the idMso
property for popular commands.
Calling something like this did the trick for me:
Application.CommandBars("Queries and Connections").Visible = True
ThisWorkbook.Activate
Application.CommandBars.ExecuteMso "RefreshAll"
Application.CalculateUntilAsyncQueriesDone
Note that I am opening the 'Queries and Connections' pane just so the user is aware of the status as it is happening. This is optional. And I do not set Application.ScreenUpdating = False
since that freezes the pane also.
I have noticed that in some Excel Versions, you might have to set all the query connections' BackgroundQuery
property to True
for the Application.CalculateUntilAsyncQueriesDone
method to work properly.
Now the error messages that Power Query Natively displays, get displayed when errors occur.
I'd also like to address the one shortcoming of this approach with a final tip. If you do not want to refresh all the queries, you can write some code to alter the WorkbookConnection.RefreshWithRefreshAll
property of the connections you do not want to execute, to stop it from executing when the 'Refresh All' button is clicked. Here is a link to the documentation of that property.
The advantage of this approach is that, when refreshing all queries, Power Query deals with the Query Dependencies while using a shared cache - so it is better/faster than triggering individual query refreshes with code.