Forum Discussion
kheldar
Dec 06, 2021Iron Contributor
Pivot Field Gets Hidden After Refresh
Hello, I've got an Excel template file with pretty basic refresh and clear table macros, and stuff. One specific pivot table's one specific field gets hidden after the source data is removed,new ...
- Dec 06, 2021
kheldar I tried looking at the file and really don't understand what you are trying to do and / or what is broken. Your macros could be simplified by having 1 macro be "ClearRows(s, t)" and then just pass those parameters for each of the cases so you call the same macro 3x instead of 3 different macros that are identical otherwise. But that doesn't address what you are asking. The 1 thing i can add is that I have had issues when updating data and refreshing having the table missing data. I found by removing a field, hitting refresh all and then adding that field back in and then refresh all again sometimes helps. I also wrote a macro that basically does a refresh and tried to find it. I think this is it:
Function ResetPivotTable(Optional PT As PivotTable) On Error Resume Next If IsMissing(PT) Or IsError(PT.Name = "") Then If (IsError(Selection.PivotTable)) Then SetPT: Set PT = ActiveSheet.PivotTables(1) Else Set PT = Selection.PivotTable End If End If On Error GoTo 0 Dim pf() 'array of page fields (filter fields) Dim pfp() 'array of current page field settings If PT.PageFields.Count <= 0 Then ResetPivotTable = PT.RefreshTable() Else i = PT.PageFields.Count ReDim pf(1 To i) ReDim pfp(1 To i) j = 0 For Each p In PT.PageFields j = j + 1 Set pf(j) = p pfp(j) = p.CurrentPage p.Orientation = xlHidden Next p If (j <> i) Or Not (PT.RefreshTable()) Then Call MsgBox("Error resetting the pivot table", , "Error Resetting Pivot Table") ResetPivotTable = False Exit Function End If For j = i To 1 Step -1 Call PT.AddFields(, , Array(pf(j)), True) PT.PageFields(pf(j).Value).CurrentPage = pfp(j) Next j End If End Function
kheldar
Dec 06, 2021Iron Contributor
Wow!! Your suggestion worked at the second try. Well I'm pretty new to VBA. I couldn't get the same macro work for different tables on different sheets 😞 Maybe you could help me do that? It'd optimize things.
mtarler
Dec 06, 2021Silver Contributor
It actually already is designed to do that by allowing a parameter to be passed to tell it which pivot table to update:
Function ResetPivotTable(Optional PT As PivotTable)
if you don't pass it anything it just performs the reset on the first pivot table on the active sheet.
If you run ResetPivotTable(aDifferentPivotTable) then it will execute on aDifferentPivotTable instead.
Function ResetPivotTable(Optional PT As PivotTable)
if you don't pass it anything it just performs the reset on the first pivot table on the active sheet.
If you run ResetPivotTable(aDifferentPivotTable) then it will execute on aDifferentPivotTable instead.