0

As per the title, looking to wait for formulae dependent on PivotTable to update before proceeding.

The set up (in Excel 2010 workbook):

  1. PivotTable constructed with
    • One xlDataField (unchanging),
    • Several xlRowFields (unchanging), and
    • One xlPageField (for changing via VBA, to increase speed of data retrieval)
  2. Two-dimensional range (columns x rows) populated using formulae (including GetPivotData derived from the above PivotTable)

Processing (via VBA):

  1. The above mentioned xlPageField is changed (expecting the values in the above mentioned range are updated)
  2. Two-dimensional range “read” into a Variant for further processing via VBA

The process is falling over between the steps 3 and 4. The range values are not updated before being read into a Variant. It could be the PivotTable is not updating, the formulae dependant on the PivotTable are not updating, or both.

Solutions attempted to date (from various StackOverlow threads or external websites):

Any guidance would be appreciated.

Requested code (truncated)

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

Regards Shannon

shansen
  • 265
  • 4
  • 14

1 Answers1

0

Can you refactor your code so that you can use the PivotTableUpdate event on the Worksheet to operate on the Pivot after it is updated?

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

  Debug.Print "Pivot table updated - do stuff..."

End Sub

You can also use the PivtoTableChangeSync event (this may be more appropriate as it discerns changes more).

Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)

  Debug.Print "Pivot table change sync..."

End Sub

More information on this event can be found here:- https://msdn.microsoft.com/EN-US/library/office/ff838251.aspx

joehanna
  • 1,471
  • 1
  • 11
  • 22
  • The xlPageField is changed some 2,500 times as part of a wider loop. I believe Worksheet_PivotTableChangeSync() would result in asynchronous code and more problems. – shansen Jul 14 '15 at 06:37
  • Can you post the actual code block that is failing? What is the data type of the Page field you are setting the value for? – joehanna Jul 14 '15 at 06:46
  • A failing code block would be: `Dim SwitchHistory As PivotTable Set SwitchHistory = Sheets("name").PivotTables("SwitchHistory") SwitchHistory.PivotCache.BackgroundQuery = False` resulting in Run-time '1004'. However, the overall (lengthy) code does not fail, it simply returns an incorrect result due to the failed refresh/update. Should the code be run twice or more on the same xlPageField the result is correct (due to no required update on the PivotTable or dependent formulae. – shansen Jul 14 '15 at 07:02
  • That isn't enough. You need to show all of the code in steps 3 & 4 so I can see what is really happening – joehanna Jul 14 '15 at 07:04