[英]How do I loop through the filter items and hide items in an Excel PivotTable using the Data Model?
我一直在使用VBA中的普通數據透視表,但我最近使用我真正喜歡的數據模型在數據透視表上找到了一些功能 - 主要是“Distinct Count”。 我在普通的數據透視表中有一些代碼,該代碼可以過濾該表以獲取“像”字符串記錄,並且可以完美地工作。 如何使用數據模型將此代碼轉換為數據透視表?
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 - 不是文本,並且數字似乎與過濾器列表中的項目數不對應。 我不知道如何訪問這些項目,因此可以使用代碼顯示或隱藏我想要的項目。 老實說,我已經很長時間沒有使用數組了。
我最終使用切片器來過濾數據。
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
Paul te Braak的這篇文章提供了大部分解決方案。 我還將本教程用於將項目保存到數組的一些幫助。 當我需要使用動態數組時, 這個stackoverflow答案也幫助了我。 感謝-GregGalloway和-jeffreyweir:在查看您提供的鏈接時,我有了使用切片器搜索解決方案的想法。
嘿,我知道這是一個老問題,但我最近提出了一個很好的解決方案:
<your other code goes here>
dim v as Variant, xlPT as Excel.PivotTable, str as String
str = vbNullString
set xlPT = xlBook.PivotTables("MyPivot")
with xlPT
for each v in .PivotFields("[table].[field]").PivotItems
select case Right(Left(v.Name, Len(v.Name) - 1), 4) 'my variables are 4-char strings
' But you can manipulate this piece to match your need
case "1234", "4312", "4321", "7462", "etc."
str = str & v.Name & ";"
end select
next v
.CubeFields("[table].[field]").PivotFields(1).VisibleItemsList = Split(str, ";")
end with
<the rest of your code goes here>
這里的關鍵知識是CubeField
對象包含一個只有1個成員的PivotFields
集合,我們可以在其中使用VisibleItemsList
。
希望這可以幫助將來的某個人。 快樂的SO'ing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.