1

所以我在excel的一张表上有多个数据透视表。我在行标签中有很长的月份列表。报告过滤器可按名称过滤。枢轴中的所有列都只是数据的总和。

我正在寻找的(我猜宏是唯一的方法),是一种能够更改该工作表上一个枢轴的过滤器并仅更新其他枢轴的方法表来模仿我更改的过滤器(报告和行标签)。其他数据透视表中的任何其他内容都不应更改 - 只是过滤器。

不幸的是,我对 vba 编码一无所知(我知道一些 java 和东西,但我从未做过任何宏编码)。我能够从互联网上复制一个宏,它完成了我需要的部分工作;它会更新报告过滤器,但不会更新行标签过滤器中的日期。这是它的编码:

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

'change all fields for all pivot tables on active sheet

For Each pfMain In ptMain.PageFields
    bMI = pfMain.EnableMultiplePageItems
        For Each pt In wsMain.PivotTables
            If pt <> ptMain Then
                pt.ManualUpdate = True
                Set pf = pt.PivotFields(pfMain.Name)
                        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(pi.Name).Visible = pi.Visible
                                    Next pi
                                    .EnableMultiplePageItems = bMI
                            End Select
                        End With
                        bMI = False

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

Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

反正有没有做我想做的事?非常感谢您的帮助!

4

1 回答 1

1

在没有看到您的数据透视表的情况下,您似乎对上述代码所需要做的就是重复设置数据透视字段值以反映日期的步骤。这是通过添加由以下行设置的循环的变体来完成的:

    set pf = pt.pivotfields(pfmain.name)

    set pf = pt.pivotfields(pfmain.nameofyourrowfiltervariable)

完整的代码如下,我也会避免错误恢复下一个

    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

    'change all fields for all pivot tables on active sheet

    For Each pfMain In ptMain.PageFields
        bMI = pfMain.enablemultiplepageitems
        For Each pt In wsMain.PivotTables
          If pt <> ptMain Then
            pt.ManualUpdate = True
            Set pf = pt.PivotFields(pfMain.Name)
                    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(pi.Name).Visible = pi.Visible
                                Next pi
                                .EnableMultiplePageItems = bMI
                        End Select
                    End With
                    bMI = False

               Set pf = pt.pivotfields(pfMain.nameofdatevariable)
               dates = pfMain.enablemultiplepageitems
               with pf
                   .clearallfilters
                   select case dates
                       case false
                               .currentpage = pfmain.currentpage.value
                       case true
                               .currentpage = "(All)"
                               for each pi in pfmain.pivotitems
                                    .pivotitems(pi.nameofdatevariable).visible = pi.visible
                                next pi
                                .enablemultiplepageitems = dates
                       end select
               end with
               date = false
               set pf = nothing
               pt.ManualUpdate = False
           End If
       Next pt
   Next pfMain

  Application.EnableEvents = True
  Application.ScreenUpdating = True

  End Sub

试试看它是否有效我没有测试过这个

于 2012-08-02T14:44:58.733 回答