2016-03-21 48 views
1

我一直在使用VBA中的普通數據透視表,但是我最近在使用我真正喜歡的數據模型的數據透視表中找到了一些功能 - 主要是「區分計數」。我有一個正常的數據透視表中的代碼,它過濾表中的記錄「喜歡」一個字符串,並且它完美地工作。如何使用數據模型將此代碼轉換爲數據透視表?如何使用數據模型遍歷篩選器項目並隱藏Excel數據透視表中的項目?

With ActiveSheet.PivotTables("Metrics").PivotFields("Reference number") 
    .Orientation = xlPageField 
    .Position = 1 
    .EnableMultiplePageItems = True 

    For i = 1 To .PivotItems.Count 
     If .PivotItems(i).Name Like "*oran*" Then 
      .PivotItems(i).Visible = False 
     End If 
    Next i 

End With 

下面是當我錄製宏並選擇項下的數據模型手動顯示所創建的代碼:

ActiveSheet.PivotTables("Metrics").PivotFields("[RawData].[Status].[Status]"). _ 
VisibleItemsList = Array("[RawData].[Status].&[apple_434]", _ 
"[RawData].[Status].&[banana_689]", _ 
"[RawData].[Status].&[orange_1346]", _ 
"[RawData].[Status].&[orange_1454]") 

這是我前進的方向,但我有有些麻煩訪問VisibleItemsList陣列:

With ActiveSheet.PivotTables("Metrics").PivotFields("[RawData].[Status].[Status]")  
    For i = 0 To UBound(.VisibleItemsList) 
     If i Like "*oran*" Then 
      i = "" 
      Debug.Print i 
     End If 
    Next i 
End With 

i的輸出爲數字0,1,2,3,4 --not文字和數字似乎不符合項在過濾器LIS數噸。我無法弄清楚如何訪問這些項目,因此我可以使用代碼顯示或隱藏我想要的項目。我會誠實地說,我從未與陣列合作過很長時間。

+0

是否必須是VBA?你可以安裝這個免費的加載項,並利用過濾器列表功能爲你做這個? http://olappivotexextend.codeplex.com/wikipage?title=Filter%20List&referringTitle=Home – GregGalloway

+0

我有人向我發出類似的請求,以迴應我發佈在http:// dailydoseofexcel上的一些非OLAP過濾代碼.com/archives/2013/11/14/filtering-pivot-based-external-ranges/ 我花了很長時間和很多黑客通過VBA完成。我正在通過代碼構建一個商業插件,它也處理複雜的通配符和排除,並且可以指向一系列搜索術語。在http://dailydoseofexcel.com/archives/2015/11/17/filtering-pivottables-with-vba-deselect-slicers-first/上預覽。在那裏留下評論,如果你想知道更多。 – jeffreyweir

+0

-GregGalloway,它必須是VBA,我沒有安裝加載項的靈活性。如果我這樣做了,那個鏈接就能解決我的問題。感謝分享。 –

回答

0

我最終使用切片器來過濾數據。

Dim sC As SlicerCache 
Dim sL As SlicerCacheLevel 
Dim aArray() As Variant 

'create a slicer cache. I just used a recorded macro to get the cache code 
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("Metrics"), _ 
    "[RawData].[Status]").Slicers.Add ActiveSheet, "[RawData].[Status].[Status]", _ 
    "Status", "Status", 141.75, 424.5, 144, 198.75 

Set sC = ActiveWorkbook.SlicerCaches("Slicer_Status") 
Set sL = sC.SlicerCacheLevels(1) 'this will start with the first item in the slicer 

    With sL 
     For i = 1 To .Count 
      If sL.SlicerItems.Item(i).Name Like "*oran*" Then 
       GoTo nextiteration 'this will skip over anything 
            'like oran when saving to the array 
      End If 

      ReDim Preserve aArray(0 To i) As Variant 
      aArray(i) = sL.SlicerItems.Item(i).Name 

      nextiteration: 
     Next i 
      sC.VisibleSlicerItemsList = aArray 'this set the visible items 
               '= to the array you just created 
      'ActiveSheet.Shapes("Status").Visible = False 
      'to hide this slicer, uncomment the line above 
    End With 

This Post by Paul te Braak提供了大部分解決方案。我還使用this tutorial來將項目保存到數組。 This stackoverflow answer在我需要使用動態數組時也幫助了我。感謝-GregGalloway和-jeffreyweir:在查看您提供的鏈接時,我想到了使用切片器搜索解決方案的想法。

相關問題