如何检查数据透视表中是否选择了多个项目?

时间:2016-10-05 22:19:17

标签: vba excel-vba pivot excel

是否可以知道在允许MultipleItems的情况下在数据透视表过滤器中检查了哪些项?

我只看到.Visible属性来检查它,但只有在不允许多个项目时它才有效。 如果允许多个项目并选中.Visible属性,则只会看到“全部”而不是所有选定的项目。

有什么办法吗?

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

结果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

2 个答案:

答案 0 :(得分:1)

请尝试下面的示例代码:

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

答案 1 :(得分:0)

我检查你的代码,我和我的结果相同。在我上周的测试中,我试过.hidden& .visible属性,如果我不选择“选择多个项目”,它可以正常工作。

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)

数据透视表是手动创建的,不是由vba创建的,我正在使用excel 2010。