2016-03-21 81 views
1

我一直在使用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数吨。我无法弄清楚如何访问这些项目,因此我可以使用代码显示或隐藏我想要的项目。我会诚实地说,我从未与阵列合作过很长时间。

+0

是否必须是VBA?你可以安装这个免费的加载项,并利用过滤器列表功能为你做这个? http://olappivotexextend.codeplex.com/wikipage?title=Filter%20List&referringTitle=Home – GregGalloway

+0

我有人向我发出类似的请求,以回应我发布在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

+0

-GregGalloway,它必须是VBA,我没有安装加载项的灵活性。如果我这样做了,那个链接就能解决我的问题。感谢分享。 –

回答

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:在查看您提供的链接时,我想到了使用切片器搜索解决方案的想法。

相关问题