Stack Overflow中有一些帖子说明了如何:
- 异步刷新所有查询
- 逐个刷新某些查询(即非异步)
如何使用VBA在Excel中刷新由Power Query查询异步生成的某些范围(* 例如,给定这些范围名称的数组 *),并在检测到这些异步刷新完成后在VBA中执行后续句子。
我尝试过的方法包括:
Sub fail_1()
'This method cannot guarantee showing the msgbox at the end of this sub AFTER the two ranges are refreshed
arrRngName = Array("rng1","rng2")
For Each itm in arrRngName
Range(itm).ListObject.QueryTable.Refresh BackgroundQuery:=True
Next itm
MsgBox "All the refreshes are done asynchronously" 'This is an example of the subsequent sentence
End Sub
Sub fail_2()
'This method cannot guarantee neither that showing the msgbox at the end of this sub AFTER the two ranges are refreshed
arrRngName = Array("rng1","rng2")
For Each itm in arrRngName
With ThisWorkbook.Connections("Query - " & itm).OLEDBConnection
.BackgroundQuery = True
.Refresh
End With
Next itm
MsgBox "All the refreshes are done asynchronously" 'This is an example of the subsequent sentence
End Sub
Sub fail_3()
'This method can guarantee showing the msgbox at the end of this sub AFTER the two ranges are refreshed, but it cannot refresh ALL the ranges at the same time(asynchronously)
arrRngName = Array("rng1","rng2")
For Each itm in arrRngName
With ThisWorkbook.Connections("Query - " & itm).OLEDBConnection
.BackgroundQuery = False
.Refresh
End With
Next itm
MsgBox "All the refreshes are done asynchronously" 'This is an example of the subsequent sentence
End Sub
1条答案
按热度按时间ma8fv8wu1#
不幸的是,没有可以用
OLEDBConnection
对象捕获的事件,因此您需要设计自己的方法。有两个简单的选择:
1.计算后
如果您的工作表中有一个表被Query修改,那么您可以处理
AfterCalculate
事件,如下所示:2. OnTime
使用计时器测试
OLEDBConnection
上的Refreshing
属性