如何使用数据模型遍历筛选项并隐藏Excel数据透视表中的项?

问题描述 投票:1回答:2

我一直在使用VBA中的普通数据透视表,但我最近使用我真正喜欢的数据模型在数据透视表上找到了一些功能 - 主要是“Distinct Count”。我在一个普通的数据透视表中有一些代码可以过滤表中的记录'Like'一个字符串,它完美地工作。如何使用数据模型将此代码转换为数据透视表?

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 - 不是文本,并且数字似乎与过滤器列表中的项目数不对应。我无法弄清楚如何访问这些项目,因此我可以使用代码显示或隐藏我想要的项目。老实说,我已经很长时间没有使用数组了。

excel vba for-loop pivot-table powerpivot
2个回答
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:在查看您提供的链接时,我有了使用切片器搜索解决方案的想法。


0
投票

嘿,我知道这是一个老问题,但我最近提出了一个很好的解决方案:

<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

© www.soinside.com 2019 - 2024. All rights reserved.