Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Dear experts.
I am doing some PoC test for a PowerQuery project via VBA. Here are the requirement
I tried to use AfterRefresh event But I couldn't get the individual query refresh status, just 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:
I found the BackgroundQuery=True must be set. otherwise, if it set to False the afterfresh event will not be triggered. I got lost....
Here are the codes:
Thanks!
Corydon
--------------------------
Option Explicit
Public WithEvents MyQuery As QueryTable
'Public s_QueryName As String
Private Sub MyQuery_AfterRefresh(ByVal Success As Boolean)
If Success Then
MsgBox (s_QueryName & " has been refreshed.")
' count = count + 1
'MsgBox (count)
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
---------------------------------------------------------------------
-------------------------------------------------------------------
Option Explicit
Dim colQueries As New Collection
Public s_QueryName As String
'Public count As Integer
Sub InitializeQueries()
Dim clsQ As clsQuery
Dim WS As Worksheet
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
s_QueryName = "Query - CombineErrorlists"
With ThisWorkbook.Connections("Query - CombineErrorlists").OLEDBConnection
'bRfresh = .BackgroundQuery
.BackgroundQuery = True
.Refresh
'.BackgroundQuery = bRfresh
End With
' MsgBox ("CombineErrorlists" & "refeesh done")
' Application.Wait DateAdd("s", 15, Now)
s_QueryName = "Query - FilesTransform"
' ActiveWorkbook.Connections("Query - FilesTransform").Refresh
With ThisWorkbook.Connections("Query - FilesTransform").OLEDBConnection
'bRfresh = .BackgroundQuery
.BackgroundQuery = True
.Refresh
'.BackgroundQuery = bRfresh
End With
' MsgBox ("FilesTransform" & " refeesh done")
End Sub
Power Query is increasingly becoming a cloud tool as it's in Power BI dataflows, datasets, and datamarts; PowerApps; Azure Data Factory; and Excel Online. In the latter case support is still limited, but it's improving. Eventually jobs like this will be done entirely in the cloud with a combination of Power Automate and OfficeScripts.
VBA is desktop bound and always will be. I'm sure there are people automating Power Query with VBA, but the lack of response to your various posts is a hint that they're few and far between. I've been working recently with one of my clients to migrate their unmaintainable VBA solution to the cloud, it comes with its own challenges but it's where Microsoft and the industry are going.
You might have better luck in an Excel forum. The intersection of people developing with Power BI and people still using VBA is getting smaller by the day. For the requirement you're describing I'd be using Power Automate.
Thanks Otravers. Actually I asked the same question at Excel forum and Stackflow 2 few monthes ago, but haven't got any feedback. I reckon the scenario I described shoule be very common. The power query does provide a good way to transform the datam but it doesn't provide enough interfaces working with MS inhouse products eg VBA. My thought was based on the traditional coding approach, considering the customized power queries as functions, using VBA to call them and get the statuses . Maybe I am wrong. There might be no seamless synergy between VBA and Power Query. I do hope MS experts here could give me a clear instruct whether my way is feasible or not. Thanks! Btw, I will try Power automate.
Appreciate this is super late, but I think this might help, it certainly helped for me (just today in fact!)
https://stackoverflow.com/questions/43548543/how-to-check-whether-connection-refresh-was-successful
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
102 | |
53 | |
21 | |
12 | |
12 |