Found a code from Andrew in http://www.mrexcel.com/forum/showthread.php?t=475636 to filter pivot from a range of data. But I encounter a problem where the loop in pivotItems seems like it can only travel upwards and have error if the new selected item is downward.
I'm populating the range from a listbox with multiple selection function.
Items sorting in pivot:
a
b
c
d
e
For eg:
1) 1st time select "c" from listbox and populate to the range - code is working
2) re-select "b" from listbox - code is working
3) if re-select again"c" or "d" or "e" - code is not working with error msg "Unable to set the visible property of PivotItem class"
Any help? Thanks
Andrew's code:
Sub PT()
'
'PT Macro
'
Dim PT As PivotTable
Dim PI As PivotItem
Set PT = Sheets("Sheet3").PivotTables("PivotTable2")
PT.PivotFields("Server").CurrentPage = "(All)"
For Each PI in PT.PivotFields("Server").PivotItems
PI.Visible = WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A10"), PI.Name) > 0
Next PI
Set PT = Nothing
End Sub
I'm populating the range from a listbox with multiple selection function.
Items sorting in pivot:
a
b
c
d
e
For eg:
1) 1st time select "c" from listbox and populate to the range - code is working
2) re-select "b" from listbox - code is working
3) if re-select again"c" or "d" or "e" - code is not working with error msg "Unable to set the visible property of PivotItem class"
Any help? Thanks
Andrew's code:
Sub PT()
'
'PT Macro
'
Dim PT As PivotTable
Dim PI As PivotItem
Set PT = Sheets("Sheet3").PivotTables("PivotTable2")
PT.PivotFields("Server").CurrentPage = "(All)"
For Each PI in PT.PivotFields("Server").PivotItems
PI.Visible = WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A10"), PI.Name) > 0
Next PI
Set PT = Nothing
End Sub