在数据透视表PageField中选择多个项目时如何获取值?

问题描述 投票:0回答:3

我正在尝试获取数据透视表(1)中的数据透视字段的值。数据透视字段,然后我尝试将过滤器值获取为字符串(str- 声明为字符串),它工作正常,但是当我在中选择多个项目时过滤器,

"(All)"
值传递给str。

如何在数据透视项中传递所选项目(多个)的值。

 For Each PF In PT1.PivotFields
 If PF.Orientation = xlPageField Then
    str = PF.CurrentPage
 PT2.PivotFields(PF.Name).CurrentPage = str

下面是我需要传递给 str 的值的快照。

enter image description here

任何帮助将不胜感激。

excel pivot-table vba
3个回答
0
投票

尝试下面的代码。首先,它检查您是否在数据透视表中启用了 MultiplePageItems,如果为 true,它会检查每个项目是否被选中(可见)。您将这些值保存在变量中或执行其他操作。

If ActiveSheet.PivotTables("table").PivotFields("field").EnableMultiplePageItems = True Then
    For i = 1 To ActiveSheet.PivotTables("table").PivotFields("field").PivotItems.Count
        If ActiveSheet.PivotTables("table").PivotFields("field").PivotItems(i).Visible = True Then 'if selected then

        'your code

        End If

    Next i

End If

0
投票
ActiveSheet.PivotTables("PivotTable1").PivotFields("field").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("field").EnableMultiplePageItems = True
For i = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems.Count
    If ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems(i).Visible = True Then
    ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems(i).Visible = False
    If ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems("item name 1 ").Visible = False Then
    ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems(i).Visible = True
    End If
    If ActiveSheet.PivotTables("PivotTable1").PivotFields("field").PivotItems("item name 2 ").Visible = False Then
    ActiveSheet.PivotTables("PivotTable1").PivotFields(field").PivotItems(i).Visible = True
    End If
   this if you repeat 3, 4, 5, etc……. 
    End If

Next i

0
投票

试试这个:

Sub marine()
    Dim PT1 As PivotTable, PT2 As PivotTable
    Set PT1 = Sheet1.PivotTables("PivotTable1")
    Set PT2 = Sheet1.PivotTables("PivotTable2")
    Dim PF As PivotField, PI As PivotItem, myfilter
    For Each PF In PT1.PageFields '~~> directly work on PageFields
        If PF.EnableMultiplePageItems = True Then '~~> check if multi-items
            For Each PI In PF.PivotItems
                If PI.Visible = True Then
                    If IsEmpty(myfilter) Then
                        myfilter = Array(PI.Caption)
                    Else
                        ReDim Preserve myfilter(UBound(myfilter) + 1)
                        myfilter(UBound(myfilter)) = PI.Caption
                    End If
                End If
            Next
        Else
            myfilter = PF.CurrentPage
        End If
        If IsArray(myfilter) Then
            PT2.PivotFields(PF.Name).EnableMultiplePageItems = True
            For Each PI In PT2.PivotFields(PF.Name).PivotItems
                If Not IsError(Application.Match(PI.Caption, myfilter, 0)) Then
                    PI.Visible = True
                Else
                    PI.Visible = False
                End If
            Next
        Else
            PT2.PivotFields(PF.Name).CurrentPage = myfilter
        End If
    Next
End Sub

看来你必须循环遍历

PT1
项目才能获得每个项目。
并且还循环遍历
PT2
项目以选择从
PT1
标识的过滤器。 HTH.
注意: 但是,如果您使用 XL 2010 及更高版本,则只需使用 Slicer

© www.soinside.com 2019 - 2024. All rights reserved.