insaneoctane
Board Regular
- Joined
- Dec 2, 2005
- Messages
- 72
I'm trying to create a macro that filters any pivot table I place my cursor on with a predetermined list (see FilterArray below). My code gives Run-time error '1004': Unable to get the PivotFields property of the PivotTable class when I place my cursor in the pivot field that I want to apply the filter. I know I'm close to making this work!! Any advice?
VBA Code:
Sub Pivot_filterCC()
Dim PT As PivotTable
FilterArray = Array("42633", "42614", "42612")
Dim myPivotField As PivotField
Set PT = ActiveSheet.PivotTables(1)
Set MyField = ActiveCell.PivotField
Set myPivotField = PT.PivotFields(MyField)
myPivotField.ClearAllFilters
myPivotField.EnableMultiplePageItems = True
numberOfElements = UBound(FilterArray) - LBound(FilterArray) + 1
If numberOfElements > 0 Then
With myPivotField
For i = 1 To myPivotField.PivotItems.Count
j = 0
Do While j < numberOfElements
If myPivotField.PivotItems(i).Name = FilterArray(j) Then
myPivotField.PivotItems(myPivotField.PivotItems(i).Name).Visible = True
Exit Do
Else
myPivotField.PivotItems(myPivotField.PivotItems(i).Name).Visible = False
End If
j = j + 1
Loop
Next i
End With
End If
End Sub