You will get this error if you try to hide all elements on any axis (rows, columns, filters). You can lure this error into your code by comparing the HiddenItems.Count
property of your HiddenItems.Count
object with the PivotField
property of the same object and make sure that you are not trying to remove the last element of the collection from the view:
So, in your case statement, you can replace with something like:
Select Case pi.Name Case Is = "1601A" pi.Visible = True Case Else If pf.HiddenItems.Count < (pf.PivotItems.Count - 1) Then pi.Visible = False Else MsgBox "Cannot hide all the items on this axis" Exit For '<~~ break the loop to stop the MsgBox popping up End If End Select
Please note that when manipulating the Excel pivot table, the last element from the axis will not be deleted - the OK button will become unavailable:
source share