I am having some very strange issues when trying to update a pivot tables pagefields using VBA. The pagefields do get updated with the correct information but the output of that is returned in the Pivot Table is not correct. When I create a new PT and use the same selection criteria I get the correct output. I seems like when I do it with VBA it corrupts the PT.
I have also tried clearing the pivot cache and refreshing the pivots prior to setting the pagefields and I still get that same issue.
At first it seemed to be working fine but once I went to validate the data the pivot table was not showing the correct data although the pagefields have been updated to the correct information. I verified this by creating another pivot table and manually selected the correct values for the pagefields and it showed the correct information. The VBA updated PT only came back with a few records where as the manually selected one brings back all of the data.
Does anyone have any idea how to get around this? I spent several hours trying to resolve and looking like all that work will be waisted.
Thanks
I have also tried clearing the pivot cache and refreshing the pivots prior to setting the pagefields and I still get that same issue.
Code:
Sub Main()
Dim ws As Worksheet
Dim wsPT As Worksheet
Dim pt As PivotTable
Set ws = ThisWorkbook.Sheets("Config")
Set wsPT = ThisWorkbook.Sheets("DiscountPivots")
Set pt = wsPT.PivotTables("PivotTable1")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Pivot Table PaigField Selectors
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
xDealSize = ws.Range("B5").Value
xLookup = ws.Range("B8").Value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Update Pivot Tables
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Set Totals Pivot
pt.PivotFields("Lookup").CurrentPage = xLookup
pt.PivotFields("Deal_Size2").CurrentPage = xDealSize
Set ws = Nothing
Set wsPT = Nothing
Set pt = Nothing
End Sub
At first it seemed to be working fine but once I went to validate the data the pivot table was not showing the correct data although the pagefields have been updated to the correct information. I verified this by creating another pivot table and manually selected the correct values for the pagefields and it showed the correct information. The VBA updated PT only came back with a few records where as the manually selected one brings back all of the data.
Does anyone have any idea how to get around this? I spent several hours trying to resolve and looking like all that work will be waisted.
Thanks