I'm trying to run a Worksheet_Change script that creates an audit log of when the last time a Power Query table has been refreshed. This table is a System of Record so its vital for users to know the last time it was refreshed. The issue seems to be that when you refresh a PQ table, Excel does not trigger Worksheet_Change because the data is replaced 'silently'. It doesn't count as a user-initiated cell change. Does anyone have a work around?
On a separate but related note, bonus points for being able to refresh a power query table using a module.
Here is my code which doesn't actually do anything:
Dim querySheet As Worksheet
Set querySheet = ThisWorkbook.Sheets("AReS Contract View Master") ' Where the PQ table lives
If Sh.Name <> querySheet.Name Then Exit Sub
Dim tbl As ListObject
On Error Resume Next
Set tbl = querySheet.ListObjects("AReS_Masters_Contract")
On Error GoTo 0
If tbl Is Nothing Then Exit Sub
If Not Intersect(Target, tbl.DataBodyRange) Is Nothing Then
Dim logSheet As Worksheet
Set logSheet = ThisWorkbook.Sheets("AReS Contract View >")
Dim nextRow As Long
nextRow = logSheet.Cells(logSheet.Rows.Count, "B").End(xlUp).Row
If nextRow < 5 Then nextRow = 5
If logSheet.Range("B" & nextRow).Value <> "" Then nextRow = nextRow + 1
logSheet.Range("B" & nextRow).Value = "Refreshed: " & Format(Now, "yyyy-mm-dd hh:nn:ss") & _
" by " & Environ("Username")
End If
End Sub