Excel 2013在多个OLAP透视表上进行过滤

时间:2016-04-06 14:39:01

标签: excel vba excel-vba olap olap-cube

VBA新手在这里。

虽然我能够使用我在网上找到的一个VBA代码同步多个本地数据透视表,但我正在努力解决以下问题:

我在一张纸上有16个OLAP连接的数据透视表。每个都有6个过滤器,其中4个过滤器都是相同的。我希望能够使用这四个过滤器之一更改所有数据透视表。

1 个答案:

答案 0 :(得分:0)

您可以为这四个字段中的每一个插入切片器。添加完成后,您可以转到切片器连接并选择所有相关的枢轴。

如果您正在寻找VBA,则必须遍历工作表上的所有枢轴,然后遍历所有报表筛选器成员并将选定的值存储到数组中。

或者您可以在隐藏选项卡上创建切片器并使用CUBERANKEDMEMBER函数返回切片器的所有选择,然后使用它来构建阵列并在任何报告过滤器时将其应用于所有数据透视表更改(您可以在工作表中使用Worksheet_PivotTableAfterValueChange过程)

以下是我们使用2个数据透视表的方式:

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
''''variables for pivottable changed
    Dim pivotname As String
    Dim pivotname2 As String
    Dim PT As PivotTable
    Dim PT2 As PivotTable

''''variables for pivotrowfields
    Dim pti As PivotItem
    Dim pti2 As PivotField

''''variables for pagefilters
    Dim PF As PivotField
    Dim colPageFields As PivotFields
    Dim aFilters As Variant

'''Un-enable events to avoid an endless loop
    Application.EnableEvents = False

'''Set variables
    pivotname = Target.Name

    If pivotname = "PivotTable1" Then
        pivotname2 = "PivotTable4"
    Else
        pivotname2 = "PivotTable1"
        pivotname = "PivotTable4"
    End If

    If ActiveSheet.Name = "Slicer Setup" Then
        Sheets("By Org Structure").Activate
    End If

    Set PT = ActiveSheet.PivotTables("" & pivotname & "")
    Set PT2 = ActiveSheet.PivotTables("" & pivotname2 & "")

    Set colPageFields = PT.PageFields

'''action for changing the filters on Page Fields
    For Each PF In colPageFields

                    PF.CubeField.EnableMultiplePageItems = True
                    'PT2.PivotFields(PF.Name).CubeField.EnableMultiplePageItems = True
                    aFilters = PT.PivotFields(PF.Name).VisibleItemsList
                    If aFilters(1) = "" Then
                        If PT.PivotFields(PF.Name).CubeField.AllItemsVisible = True Then
                            PT2.PivotFields(PF.Name).ClearAllFilters
                        Else
                            PT2.PivotFields(PF.Name).VisibleItemsList = Array("")
                        End If
                    Else
                        PT2.PivotFields(PF.Name).VisibleItemsList = Array(aFilters)
                    End If

    Next

Set colPageFields = PT.RowFields

'''action for changing the filters on Row Fields
    For Each PF In colPageFields
                     PF.CubeField.EnableMultiplePageItems = True
                    'PT2.PivotFields(PF.Name).CubeField.EnableMultiplePageItems = True
                    aFilters = PT.PivotFields(PF.Name).VisibleItemsList
                    If aFilters(1) = "" Then
                        If PT.PivotFields(PF.Name).CubeField.AllItemsVisible = True Then
                            PT2.PivotFields(PF.Name).ClearAllFilters
                        Else
                            PT2.PivotFields(PF.Name).VisibleItemsList = Array("")
                        End If
                    Else
                        PT2.PivotFields(PF.Name).VisibleItemsList = Array(aFilters)
                    End If
    Next


'''Re-enable events to allow for this event macro to be kicked off again next time pivottables are changed
    Application.EnableEvents = True

    ActiveWorkbook.ShowPivotTableFieldList = False

End Sub