呼叫宏AfterRefresh问题

时间:2019-07-10 12:07:54

标签: excel vba powerquery

我希望刷新查询后出现vbOK消息框。

我已经在这里完成了一些答案,并试图将其拼凑在一起,但是我无法弄清自己在做错什么。

这在ThisWorkbook中:

Dim qtevent As qtclass
-------------------------------------------------

Private Sub Workbook_Open()

Set qtevent = New qtclass
Set qtevent.HookedTable = 
ThisWorkbook.Worksheets("TagData").ListObjects("barTagData").QueryTable

End Sub
-------------------------------------------------

Sub refreshData()

Refresh_Note.Show vbModeless

Application.ScreenUpdating = True
Application.DisplayAlerts = False

With ThisWorkbook
.Connections("Query - PSTab").OLEDBConnection.BackgroundQuery = True
.Connections("Query - PETab").OLEDBConnection.BackgroundQuery = True
.Connections("Query - barTagData").OLEDBConnection.BackgroundQuery = True

.Connections("Query - PSTab").Refresh
.Connections("Query - PETab").Refresh
.Connections("Query - barTagData").Refresh

End With

DoEvents

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
-------------------------------------------------

这是qtclass模块:

Option Explicit

Public WithEvents qt As 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 successfully."
If Success = True Then
Call hideForm
MsgBox "If called Successfully"
End Sub
-------------------------------------------------

Sub hideForm()

Refresh_Note.Hide

End Sub
-------------------------------------------------

需要任何帮助! :)谢谢。

在刷新查询后,我尝试调用hideForm子项,这也会产生另一个msg框,说明“查询已刷新”。这部分已经准备好了,但是该子级没有被称为AfterRefresh。

1 个答案:

答案 0 :(得分:0)

只有一种方法可以做到这一点。对于所有Query - PSTabQuery - PETabQuery - barTagData查询,您需要在查询属性窗口中取消选中“启用后台刷新”选项。

完成此操作后,DoEvents函数应等待,直到刷新查询。因此,您可以在MsgBox

之后添加一个DoEvents函数

Disable Query refresh