How to get the refresh status of each power query of a excel workbook via VBA?

corydon

New Member
Joined
Mar 11, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Dear experts, I am doing some PoC test for a PowerQuery project via VBA. Here are the requirement
1. Need to refresh the queries in a given order
2. The query behind needs to start once the previous query finishes and successes, if fails, need to provide error reason. Msgbox(query name, refresh status, error reason if applied)

I tried to use AfterRefresh event But I couldn't get the individual query refresh status, but overall status. And I tried to disable the background fresh , but didn't find a way if it worked or not. Please could I have your help. Thanks in advance, Here are the codes:

1. Class Modules( I expect to show the queryname in the Msgbox, how could add ?)
------------------------------------------------------------------------
Option Explicit

Public WithEvents MyQuery As QueryTable

Private Sub MyQuery_AfterRefresh(ByVal Success As Boolean)
If Success Then
MsgBox "Query has been refreshed."

Else

End If
End Sub

Private Sub MyQuery_BeforeRefresh(Cancel As Boolean)
If MsgBox("Refresh query?", vbYesNo) = vbNo Then
Cancel = True

Else

End If
End Sub
---------------------------------------------------------------------


2. Modules
-------------------------------------------------------------------
Dim QT As QueryTable
Dim LO As ListObject

For Each WS In ThisWorkbook.Worksheets
For Each QT In WS.QueryTables
MsgBox (WS.Name)
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ
Next QT


For Each LO In WS.ListObjects
MsgBox (WS.Name)
Set QT = LO.QueryTable
Set clsQ = New clsQuery
Set clsQ.MyQuery = QT
colQueries.Add clsQ

Next LO
Next WS


End Sub


Private Sub Button1_Click()
Call InitializeQueries
'ActiveWorkbook.Connections("Query - CombineErrorlists").Refresh

Dim bRfresh As Boolean

With ThisWorkbook.Connections("Query - CombineErrorlists").OLEDBConnection
bRfresh = .BackgroundQuery
.BackgroundQuery = False
.Refresh
.BackgroundQuery = bRfresh

End With



' Application.Wait DateAdd("s", 15, Now)
ActiveWorkbook.Connections("Query - FilesTransform").Refresh

ActiveWorkbook.Connections("Query - thirdQuery").Refresh


End Sub

----------------------------------------------------------------------------------------------------
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

I found the BackgroundQuery=True must be set. otherwise, if it set to False the afterfresh event will not be triggered. I got lost...​

 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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