根据标题,在继续之前等待依赖于数据透视表的公式进行更新。
设置(在Excel 2010工作簿中):
处理(通过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
DoEvents
PivotTables("*name*").RefreshTable
DoEvents
Application.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
此致 香农
答案 0 :(得分:0)
您是否可以重构代码,以便在更新后可以使用PivotTableUpdate
上的Worksheet
事件对Pivot进行操作?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Debug.Print "Pivot table updated - do stuff..."
End Sub
您还可以使用PivtoTableChangeSync
事件(这可能更合适,因为它可以更多地识别更改)。
Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
Debug.Print "Pivot table change sync..."
End Sub
有关此活动的更多信息,请访问: - https://msdn.microsoft.com/EN-US/library/office/ff838251.aspx