使用VBA更新数据透视表过滤器

时间:2019-03-29 13:26:37

标签: excel vba filter pivot-table

我正在尝试根据具有不同数据源的另一张纸上的另一个数据透视表的过滤器来过滤数据透视表。

我有两张纸(主要和实际尺寸-样式),并想按“样式”进行过滤。我要在主表中选择要过滤的样式,并希望在那里过滤其他样式以更新其他工作表(如果通过按钮进行过滤,将有帮助)。我更改了一些代码以说明要过滤的字段(样式)。现在,我将“主工作表”数据透视表重置为在“尺寸实际-样式”上选择了样式过滤器时显示所有值。我需要进行切换,而不是清除过滤器,而是希望它使用过滤器中选定的样式进行更新。

Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
On Error Resume Next
Dim wsMain As Worksheet
Dim ws As Worksheet
Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean

On Error Resume Next
Set wsMain = ActiveSheet
Set ptMain = Target

Application.EnableEvents = False
Application.ScreenUpdating = False

For Each pfMain In ptMain.PageFields
bMI = pfMain.EnableMultiplePageItems
For Each ws In ThisWorkbook.Worksheets
    For Each pt In ws.PivotTables
        If ws.Name & "_" & pt <> wsMain.Name & "_" & ptMain Then
            pt.ManualUpdate = True
            Set pf = pt.PivotFields("Style")
                    bMI = pfMain.EnableMultiplePageItems
                    With pf
                        .ClearAllFilters
                        Select Case bMI
                            Case False
                                .CurrentPage = pfMain.CurrentPage.Value
                            Case True
                                .CurrentPage = "(All)"
                                For Each pi In pfMain.PivotItems
                                    .PivotItems("Style").Visible = 
pi.Visible
                                Next pi
                                .EnableMultiplePageItems = bMI
                        End Select
                    End With
                    bMI = False

            Set pf = Nothing
            pt.ManualUpdate = False
        End If
    Next pt
Next ws
Next pfMain

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

我正在尝试让第二张工作表上的过滤器根据第一张工作表样式过滤器上的选择来更新其过滤器。

0 个答案:

没有答案