neu*_*lly 8 excel vba refresh doevents powerquery
建立:
问题描述
使用任何技术的任意组合后,等待Power Query完成刷新,如下面所示的"我尝试过的东西:"部分所述.根据"查询和连接"窗格中显示的刷新指示器(微调器?)图标,可以在Power Query表完成更新之前显示消息框并执行任何其他代码.
上述语句的例外是Application类的"OnTime"方法,如下面的"代码"部分所示,它似乎不会中断电源查询刷新的轮询.但是,此方法的问题在于它使用硬编码的时间来暂停VBA代码,并且这并不总是有效,因为查询的数据的大小,数量和持续时间将随时间而变化.
我尝试过的事情:
码:
Private Sub sht_sub_Refresh_AllConnections_dev()
'Name: sht_sub_Refresh_AllConnections_dev
'Purpose: An attempt at using VBA to wait for Queries to finish updating before displaying a message.
'Description: Waits for a hard coded period of time before dislpaying the message box.
'State: WIP.
'Dev: Needs a way to look at the connection stream to somehow detect when its finished.
'DECLARATIONS:
'------------'
Dim procName As String 'Stores this procedure's name.
Dim qTblLst As QueryTables 'A query table collection object.
Dim qTblObj As QueryTable 'A query table object.
Dim conLst As Connections 'A connection collection object.
Dim conObj As WorkbookConnection 'A connection object.
Dim idx As Long 'A loop counter.
'INITIALIZATIONS:
'---------------'
procName = "sht_sub_Refresh_AllConnections_dev" 'Store this procedure's name.
Linit.ini_Setup_Project 'Setup the project if needed.
Set conLst = ThisWorkbook.Connections 'Set the connections list object.
Set conObj = conLst.Item(conLst.Count) 'Set an initial connection object.
idx = 0 'As an exit if the do loop continues without end.
'MAIN CODE BODY:
'--------------'
'Turn off backgroundquery for each connection type.
For Each conObj In conLst 'For each connection object,
With conObj
Select Case .Type 'Check the connection type,
Case 1 'If its an OLEDB connection then,
.OLEDBConnection.BackgroundQuery = False 'Set it's backgroundquery property to false.
Case 2 'If its an ODBC connection the,
.ODBCConnection.BackgroundQuery = False 'Set it's backgroundquery property to false.
End Select
End With
Next conObj
ThisWorkbook.RefreshAll 'Refresh all connections.
'DEV: Using loops, DoEvents and a query name starting with the letters "zzzz" as suggsted here:
'https://social.technet.microsoft.com/Forums/en-US/bc3f7748-8a52-498d-951c-4566b8adf45a/in-excel-2016-power-queries-dont-refresh-in-the-background-anymore?forum=powerquery
'and here:
'https://www.myonlinetraininghub.com/excel-forum/vba-macros/pause-macro-until-power-queries-finished-refreshing
'Attempt to wait until the last connection has finished refreshing.
Do Until Linit.gvTbl_ZZZZZ.QueryTable.Refreshing = True 'Wait until the last table starts refreshing,
idx = idx + 1 'Icrement a loop count,
If idx > 3000 Then Exit Do 'If the loop goes longer then 3000 iterations exit,
Loop 'otherwise continue waiting.
VBA.DoEvents 'Do events before continueing (doens't work).
Do Until Linit.gvTbl_ZZZZZ.QueryTable.Refreshing = False 'Wait until the last table finishes refreshing,
idx = idx + 1 'Icrement a loop count,
If idx > 3000 Then Exit Do 'If the loop goes longer then 3000 iterations exit,
Loop 'otherwise continue waiting.
VBA.DoEvents 'Do events before continueing (doens't work).
'DEV: The following is an attempt to get connections to
' finish refreshing before code continues as suggested here:
'https://stackoverflow.com/questions/22083668/wait-until-activeworkbook-refreshall-finishes-vba
Application.CalculateUntilAsyncQueriesDone 'This is placed here as well as after the refresh.
VBA.DoEvents 'Do events before continueing (doens't work).
Application.EnableEvents = False 'Maybe turning off events helps? (nope...),
Application.ScreenUpdating = False 'This is reset in the procedure called as an argument to the next line:
Application.OnTime DateAdd("s", 3, Now), _
"Lwksh.sht_sub_Msg_RefreshDone" 'The called procedure just displays a message box.
Application.EnableEvents = True 'Restore events,
Application.ScreenUpdating = True 'Restore screen updating.
'MEMORY CLEANUP:
'--------------'
EXIT_CLEAN:
procName = Empty
Set qTblLst = Nothing
Set qTblObj = Nothing
Set conLst = Nothing
Set conObj = Nothing
idx = 0
End Sub
Run Code Online (Sandbox Code Playgroud)
代码说明:
问题
感谢您抽出时间和关注此问题.如果我忘记包含任何信息或者可能需要重新编写某些内容以使此问题更加清晰,请在评论中告诉我.
Mik*_*ney -1
我正在调用 ThisWorkbook.Connections.Ranges(1).ListObject.QueryTable 上的 .Refresh 方法,并且使用 BackgroundQuery:=False。它看起来相当可靠 - 到目前为止,我的宏可能已调用该方法超过 10,000 次,并且错误/冻结率非常低。
据我了解,当 PQ 在 Excel 2016 的对象模型中正确公开时,所有这些 DoEvents 解决方法等都变得过时。
这是一个粗略的代码示例:
Dim cn As WorkbookConnection
For Each cn In ThisWorkbook.Connections
cn.Ranges(1).ListObject.QueryTable.Refresh BackgroundQuery:=False
Next cn
Run Code Online (Sandbox Code Playgroud)
在 For 循环内,您可以检查 cn.Name 来控制单个查询的执行。名称属性位于工作簿连接名称之后,例如“查询 - ”和 PQ 查询名称。
| 归档时间: |
|
| 查看次数: |
4902 次 |
| 最近记录: |