问题
Is it possible to know which items are checked in a PivotTable filter in case MultipleItems
are allowed?
I only see .Visible
property to check it but it only works if multiple items are not allowed.
If multiple items are allowed and checked .Visible
property, you only see "ALL" instead of all items selected.
Any way to do it?
Dim pvt As PivotTable
Dim fld As PivotField
Dim itm As PivotItem
Dim flt As PivotFilter
Dim i As Integer
Set xbFuente = ThisWorkbook
Set xlDatos = xbFuente.Worksheets("TABLAS")
Set pvt = xlDatos.PivotTables("MAIN")
pvt.ManualUpdate = True
Application.EnableEvents = False
Application.ScreenUpdating = False
If pvt.ShowPageMultipleItemLabel = True Then
Debug.Print "The words 'Multiple Items' can be displayed."
End If
For Each fld In pvt.PageFields
Debug.Print fld.Name & " -- " & fld.Orientation & " -- " & fld.EnableItemSelection & " -- " & fld.EnableMultiplePageItems & " -- "
If fld.AllItemsVisible = True Then
' If all items are visible "ALL"
For Each itm In fld.VisibleItems
Debug.Print "---- ALLITEMSVISIBLE TRUE --" & "VISIBLE" & " -- " & itm.Name & " -- " & itm.Visible
Next
Else
For Each itm In fld.VisibleItems
Debug.Print "---- ALLITEMSVISIBLE FALSE --" & "VISIBLE" & itm.Name & " -- " & itm.Visible
Next
For Each itm In fld.HiddenItems
Debug.Print "--------ALLITEMSVISIBLE FALSE --" & "HIDDEN -- " & itm.Name & " -- " & itm.Visible
Next
For Each itm In fld.PivotItems
Debug.Print "--------ALLITEMSVISIBLE FALSE --" & "HIDDEN -- " & itm.Name & " -- " & itm.Value
Next
End If
Next
Result:
Warranty Flag -- 3 -- Verdadero -- Verdadero --
---- ALLITEMSVISIBLE FALSE --VISIBLE(All) -- Verdadero
--------ALLITEMSVISIBLE FALSE --HIDDEN -- A -- Falso
--------ALLITEMSVISIBLE FALSE --HIDDEN -- I -- Falso
--------ALLITEMSVISIBLE FALSE --HIDDEN -- O -- Falso
--------ALLITEMSVISIBLE FALSE --HIDDEN -- P -- Falso
回答1:
Try the sample code below:
Sub Check_PivotFilter_Selection()
Dim pvt As PivotTable
Dim fld As PivotField
Dim itm As PivotItem
Dim flt As PivotFilter
Dim i As Integer
Set xbFuente = ThisWorkbook
Set xlDatos = xbFuente.Worksheets("TABLAS")
Set pvt = xlDatos.PivotTables("MAIN")
pvt.ManualUpdate = True
Application.EnableEvents = False
Application.ScreenUpdating = False
If pvt.ShowPageMultipleItemLabel = True Then
Debug.Print "The words 'Multiple Items' can be displayed."
End If
For Each fld In pvt.PageFields
Debug.Print fld.Name & " -- " & fld.Orientation & " -- " & fld.EnableItemSelection & " -- " & fld.EnableMultiplePageItems & " -- "
' loop through all items in Field, and check which ones are Selected (and which ones are not)
For Each itm In fld.PivotItems
If itm.Visible = True Then
Debug.Print " Item " & itm.Name & " in Field " & fld.Name & " is Visible (Selected) "
Else
Debug.Print " Item " & itm.Name & " in Field " & fld.Name & " is Hidden (Not Selected) "
End If
Next itm
Next fld
End Sub
回答2:
i check your code and i have same result as mine. In my tests last week i tried with .hidden & .visible properties and it works fine if i do not select "multiple items selected".
Warranty Flag -- 3 -- Verdadero -- Verdadero --
Item I in Field Warranty Flag is Hidden (Not Selected)
Item O in Field Warranty Flag is Hidden (Not Selected)
Item IW in Field Warranty Flag is Hidden (Not Selected)
Pivot table was created manually, not created by vba and i am working with excel 2010.
来源:https://stackoverflow.com/questions/39884591/how-to-check-if-multiple-items-are-selected-in-a-pivot-table