2016-04-06 133 views
0

VBA新手在這裏。對多個OLAP數據透視表進行Excel 2013篩選

雖然我們能夠使用我在網上找到了VBA代碼之一同步多個本地數據透視表,我正在與以下掙扎:

我有一個片16個OLAP連接樞軸表。每個都有6個過濾器,其中4個都是相同的。我希望能夠使用這四個過濾器中的一個來更改所有數據透視表。

回答

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 
+0

看來,作爲透視表已分組域我不能鏈接切片機基於OLAP。我是否正確地假設我需要調整所有數據透視表並在調整VBA代碼時爲其複製代碼? – onevba

+0

它的世界可能會更乾淨,更短,以循環通過for或每個循環。 –

相關問題