VBA-数据透视表的循环过滤数据

时间:2019-03-29 07:43:29

标签: excel vba

由于问题的上下文已更改,原始帖子已删除。

我有一个数据透视表,我试图对其进行过滤,然后仅捕获过滤的数据,但是,我似乎没有捕获正确的过滤器数据。

以下是原始表格:

enter image description here

数据透视表:

enter image description here

已应用过滤器:

enter image description here

数据透视表:

enter image description here

不,当我运行代码时,它不会捕获我所过滤的数据,它仍在读取fk_new_component的所有日期。

代码:

    Sub Results_06_Filter_Pivot_Table(myArray As Variant)

        Set tmpltWkbk = Workbooks("New DB.xlsm")
        Set ws1 = tmpltWkbk.Sheets("Pivot")
        Set PvtTbl = ws1.PivotTables("PivotTable")

'Array of fk_keyword unique values
        myArr = myArray

        Application.ScreenUpdating = False

        PvtTbl.ManualUpdate = True

        Set pvtField = PvtTbl.PivotFields("fk_keyword")

        With pvtField
            .Orientation = xlPageField
            .Position = 1
        End With

        PvtTbl.ManualUpdate = False

        ws1.Activate

        For i = LBound(myArr) To UBound(myArr)

            If i <> 0 Then

                pvtField.ClearAllFilters

                pvtField.CurrentPage = myArr(i)

                With ws1

                    Set compField = PvtTbl.PivotFields("fk_new_component")

                    'For Each PvtItem In PvtTbl.VisibleFields
                    For Each Pi In compField.PivotItems

                        If Pi <> "" And Pi.Visible Then

                            i = i + 1
                            ReDim Preserve arrCompElements(i)
                            arrCompElements(i) = PvtItem.Name

                        End If

                    Next Pi

                End With

            End If

        Next i

        'pvtField.ClearAllFilters

        For Each pvtField In PvtTbl.PageFields
          pvtField.Orientation = xlHidden
        Next pvtField

        Application.ScreenUpdating = True

        PvtTbl.ManualUpdate = False

    End Sub

0 个答案:

没有答案