【问题标题】:Linking slicers from PivotTables built with data model从使用数据模型构建的数据透视表中链接切片器
【发布时间】:2015-12-05 19:50:02
【问题描述】:

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

使用 VBA,我的想法是从 pivotcache1(我的主切片器链接到的缓存)填充所有可见数据透视项的数组,然后循环通过这个数组来过滤我的其他缓存(pivotcache2 和 pivotcache3)。

但是,下面的代码不起作用 - 它在 For Each 循环处发生故障。

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

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

任何人都可以确认这一点和/或向我提供过滤所有数据模型缓存的代码吗?

【问题讨论】:

    标签: vba excel datamodel


    【解决方案1】:

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

    下面是一个工作示例。

    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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-13
      • 2018-11-28
      • 2023-02-14
      • 2014-03-29
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多