How to call macro after Refresh or Refresh All button pressed?

前端 未结 2 1646

Ultimately, I would like to run a macro after anyone refreshes the workbook, specifically using the Refresh button under the Data tab in Excel.

For the time being, I

2条回答
  •  被撕碎了的回忆
    2020-12-09 05:18

    You haven't actually connected the querytable to the class instance. Revised qtclass

    Option Explicit
    
    Private WithEvents qt As Excel.QueryTable
    Public Property Set HookedTable(q As Excel.QueryTable)
        Set qt = q
    End Property
    
    Private Sub qt_AfterRefresh(ByVal Success As Boolean)
    
        MsgBox "qt_AfterRefresh called sucessfully."
        If Success = True Then
            Call Module2.SlicePivTbl
            MsgBox "If called succesfully."
        End If
    
    End Sub
    
    Private Sub qt_BeforeRefresh(Cancel As Boolean)
        MsgBox "qt_BeforeRefresh called."
    End Sub
    

    New ThisWorkbook code:

    Dim qtevent As qtclass
    Private Sub Workbook_Open()
    
        Set qtevent = New qtclass
        Set qtevent.HookedTable = ThisWorkbook.Worksheets("Data-Fund").ListObjects(1).QueryTable
    
    End Sub
    

    Note that this is quite closely coupled. It would be more re-usable if you were to raise events in the class and declare your qtevent variable WithEvents.

提交回复
热议问题