【发布时间】:2015-07-14 15:13:30
【问题描述】:
根据标题,希望等待依赖数据透视表的公式更新后再继续。
设置(在 Excel 2010 工作簿中):
- 数据透视表使用
- 一个 xlDataField(不变),
- 几个 xlRowFields(不变)和
- 一个 xlPageField(用于通过 VBA 更改,以提高数据检索速度)
- 使用公式(包括从上述数据透视表派生的 GetPivotData)填充的二维范围(列 x 行)
处理(通过 VBA):
- 上面提到的 xlPageField 已更改(期望中的值 上述范围已更新)
- 将二维范围“读取”到 Variant 中,以便通过 VBA 进行进一步处理
该过程在步骤 3 和 4 之间失败。范围值在读入 Variant 之前不会更新。可能是数据透视表未更新,依赖于数据透视表的公式未更新,或两者兼而有之。
迄今为止尝试的解决方案(来自各种 StackOverlow 线程或外部网站):
-
PivotTables("*name*").PivotCache.BackgroundQuery = False(Getting Excel VBA to wait for external workbook links to refresh before continuing)。导致运行时错误“1004”。 -
Activeworkbook.RefreshAll DoEvents(Wait until ActiveWorkbook.RefreshAll finishes - VBA) 没有影响。 - 还有以下几种:
ThisWorkbook.RefreshAll DoEventsPivotTables("*name*").RefreshTable DoEventsApplication.Calculate DoEvents-
Do Until Application.CalculationState = xlDone DoEvents Loop(Wait until Application.Calculate has finished)
任何指导将不胜感激。
请求的代码(截断)
Dim AllocationRange As Variant
Dim SwitchHistory As PivotTable
Sub Main()
InitialisePublicVariables
'For each member
GetMemberSwitchHistory
Dim Allocations As New Dictionary: Set Allocations = GetDictionary("Allocations")
// further processes
'Next member
End Sub
Private Sub InitialisePublicVariables()
Set SwitchHistory = Sheets("All Switches (clean)").PivotTables("SwitchHistory")
AllocationRange = Range("AllocationRange")
End Sub
Private Sub GetMemberSwitchHistory()
Dim MemberAccountNumber As String: MemberAccountNumber = Range("MemberAccountNumber").Value
SwitchHistory.PivotFields("Member Account Number").CurrentPage = MemberAccountNumber
SwitchHistory.RefreshTable
DoEvents
End Sub
问候 香农
【问题讨论】:
标签: vba excel pivot-table