我一直在使用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數噸。我無法弄清楚如何訪問這些項目,因此我可以使用代碼顯示或隱藏我想要的項目。我會誠實地說,我從未與陣列合作過很長時間。
是否必須是VBA?你可以安裝這個免費的加載項,並利用過濾器列表功能爲你做這個? http://olappivotexextend.codeplex.com/wikipage?title=Filter%20List&referringTitle=Home – GregGalloway
我有人向我發出類似的請求,以迴應我發佈在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
-GregGalloway,它必須是VBA,我沒有安裝加載項的靈活性。如果我這樣做了,那個鏈接就能解決我的問題。感謝分享。 –