首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从使用数据模型构建的PivotTables链接切片器

从使用数据模型构建的PivotTables链接切片器
EN

Stack Overflow用户
提问于 2015-09-10 02:02:52
回答 1查看 1.2K关注 0票数 0

我有几个使用不同缓存的数据透视表。所有这些表都是通过数据连接创建的,并且都使用数据模型。我正在尝试创建主切片器,同时过滤所有的表。

使用VBA,我的想法是填充来自pivotcache1 (我的主切片器链接到的缓存)的所有可见pivotitems的数组,然后循环该数组以过滤我的其他缓存(pivotcache2和pivotcache3)。

然而,下面的代码不起作用-它在For Each循环中崩溃。

代码语言:javascript
运行
复制
Sub FilterMonth()

    Dim sc1, sc2, sc3 As SlicerCache
    Dim si As SlicerItem

    Set sc1 = ActiveWorkbook.SlicerCaches("Slicer_ClickMonth")
    Set sc2 = ActiveWorkbook.SlicerCaches("Slicer_Visit_Month1")
    Set sc3 = ActiveWorkbook.SlicerCaches("Slicer_Visit_Month")

    For Each si In sc1.SlicerItems
        If si.Selected Then
            'Add to my array
        End If
    Next 

End Sub

我相信这是因为在数据模型上构建的缓存的语法是不同的。

有人能证实这一点和/或提供给我代码来过滤我所有的数据模型缓存吗?

EN

回答 1

Stack Overflow用户

发布于 2015-09-10 02:53:04

根据我在这里找到的信息,SlicerItems属性不能直接用于连接到数据模型( https://msdn.microsoft.com/EN-US/library/office/ff839561.aspx数据源)的数据透视表。相反,我需要对SlicerCacheLevels对象使用SlicerItems。

下面是一个工作示例。

代码语言:javascript
运行
复制
Sub SlicerMonth()

    Dim sc1, sc2, sc3 As SlicerCache
    Dim si As SlicerItem
    Dim sl1 As SlicerCacheLevel
    Dim mnth, nokey, nomatch As String

    Set sc1 = ActiveWorkbook.SlicerCaches("Slicer_ClickMonth")
    Set sc2 = ActiveWorkbook.SlicerCaches("Slicer_Visit_Month1")
    Set sc3 = ActiveWorkbook.SlicerCaches("Slicer_Visit_Month")

    Set sl1 = sc1.SlicerCacheLevels(1)

    'Clears the strings
    nokey = ""
    nomatch = ""

    'Cycles through the Master Slicer and populates the strings
    For Each si In sl1.SlicerItems
        If si.Selected Then
            mnth = Right(si.Name, 7) '7 becaucse I always have 3 letter months
            nokey = nokey & ",[No Key].[Visit Month]" & mnth
            nomatch = nomatch & ",[No Match].[Visit Month]" & mnth
            i = i + 1
        End If
    Next

    'Trims off the first comma for the strings
    nokey = Right(nokey, Len(nokey) - 1)
    nomatch = Right(nomatch, Len(nomatch) - 1)

    'Clears the filter in the slicers
    sc2.ClearManualFilter
    sc3.ClearManualFilter

    'Sets the slicers to match the Master slicer
    sc2.VisibleSlicerItemsList = Array(Split(nokey, ","))
    sc3.VisibleSlicerItemsList = Array(Split(nomatch, ","))


End Sub

唯一值得注意的另一个问题(我花了大约半天时间才弄明白)是确保在创建字符串(nomatch & nokey)时逗号后面没有空格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32486391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档