This is baffling us. I have a standard pivot table with a report filter on it that allows multiple selection of items. I can get the selected items in the report filter with:
After researching this for a bit, everything I have found is pointing at this being a bug in the VBA library code. However, I did seem to (accidently) stumble across a method that appears to fix the error, that I did not see mentioned in any other threads / forums about this error.
The pivot table I was working with basically looked like this:
and I was trying to have a macro hide the highlighted sections. Note that the error was only being thrown for the first highlighted section, which contained blank dates / data.
I was using this code:
Function RemovePivotTableBlanks(ByRef pt As PivotTable, ByVal field As String)
Dim pi As PivotItem
For Each pi In pt.PivotFields(field).PivotItems
If Left(pi.Value, 1) = "<" Or Left(pi.Value, 1) = ">" Or pi.Value = "(blank)" Then
'this throws an error, but only for the first PivotItem
If pi.Visible = True Then
pi.Visible = False
End If
End If
Next pi
End Function
Also note that the .Visible
property still showed the correct value in the Locals window.
For some reason, if I assigned the .Value
to a string
, and back to the PivotItem
object, the error went away:
Function RemovePivotTableBlanks(ByRef pt As PivotTable, ByVal field As String)
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim pi As PivotItem
Dim str As String
For Each pi In pt.PivotFields(field).PivotItems
If Left(pi.Value, 1) = "<" Or Left(pi.Value, 1) = ">" Or pi.Value = "(blank)" Then
str = pi.Value
pi.Value = str
'no error for the first PivotItem anymore
If pi.Visible = True Then
pi.Visible = False
End If
End If
Next pi
Application.EnableEvents = True
Application.ScreenUpdating = True
End Function