真的没有办法统计连接到 OLAP 数据源的 Slicer 中的 Visible Slicer Item 的数量吗?
Is there really no way to count the number of Visible Slicer Items in a Slicer connected to an OLAP data source?
我试图循环遍历三个切片器的所有切片器项目,然后在最内层循环中调用另一个子程序。切片器连接到 OLAP 数据源。对于每个切片器,我只希望它循环遍历可见的切片器项目。从外部切片器中选择一个项目后,我隐藏了没有数据的项目。如果我使用下面的代码,那么它将遍历所有切片器项目,而不仅仅是可见的:
'Begin Loop through Fund Name Slicer
i = 1
Do Until i = ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.count + 1
'select ith item in Fund Name Slicer
ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.SlicerItems(i).Name)
'hide items with no data in Scenario Name Slicer
With ActiveWorkbook.SlicerCaches(SC2).Slicers(S2)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Scenario Name Slicer
j = 1
Do Until j = ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.count + 1
'select jth item in Scenario Name Slicer
ActiveWorkbook.SlicerCaches(SC2).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.SlicerItems(j).Name)
'hide items with no data in Override Set Name Slicer
With ActiveWorkbook.SlicerCaches(SC3).Slicers(S3)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Override Set Name Slicer
k = 1
Do Until k = ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.count + 1
'Select kth item in Override Set Name Slicer
ActiveWorkbook.SlicerCaches(SC3).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.SlicerItems(k).Name)
'Call sub that copies and pastes summary values
Call SelectedComboOnly
k = k + 1
Loop
j = j + 1
Loop
i = i + 1
Loop
我尝试用类似的东西替换 Do Until 语句:
Do Until i = ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItems.count + 1
但这会引发 运行 时间错误。我发现 here 那 "Attempting to access the VisibleSlicerItems property for slicers that are connected to an OLAP data source (SlicerCache.OLAP = True) generates a run-time error." 真的没有办法在这里访问可见切片器项目的数量吗?或者我应该尝试一些完全不同的东西吗?我的宏完成了它应该做的事情,但大约需要 25 分钟。我只是试图 运行 通过仅可见的切片器项目来优化代码。这将使它必须 运行 从 1,458 到 84 的组合。我已经关闭了屏幕更新。
尝试使用这个逻辑:
Sub Test()
Dim SC As SlicerCache
Dim SI As SlicerItem
For Each SC In ThisWorkbook.SlicerCaches 'this will loop through all your slicer caches in the whole workbook
'if you want to skip an slicer do it here and skip to the next
For Each SI In SC.VisibleSlicerItems 'this will loop through all your visible slicer items
'some code
Next SI
Next SC
End Sub
抱歉,回答晚了,但我只想分享以下对我有用的解决方案:
UBound(ThisWorkbook.SlicerCaches(SC1).SlicerCacheLevels(1).VisibleSlicerItemsList)
编辑:
.VisibleSlicerItemsList 属性 returns 一个数组,因此您不能像对集合那样在其上使用 .count 属性。为了获得数组的长度,使用函数 UBound (see here).
我试图循环遍历三个切片器的所有切片器项目,然后在最内层循环中调用另一个子程序。切片器连接到 OLAP 数据源。对于每个切片器,我只希望它循环遍历可见的切片器项目。从外部切片器中选择一个项目后,我隐藏了没有数据的项目。如果我使用下面的代码,那么它将遍历所有切片器项目,而不仅仅是可见的:
'Begin Loop through Fund Name Slicer
i = 1
Do Until i = ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.count + 1
'select ith item in Fund Name Slicer
ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.SlicerItems(i).Name)
'hide items with no data in Scenario Name Slicer
With ActiveWorkbook.SlicerCaches(SC2).Slicers(S2)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Scenario Name Slicer
j = 1
Do Until j = ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.count + 1
'select jth item in Scenario Name Slicer
ActiveWorkbook.SlicerCaches(SC2).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.SlicerItems(j).Name)
'hide items with no data in Override Set Name Slicer
With ActiveWorkbook.SlicerCaches(SC3).Slicers(S3)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Override Set Name Slicer
k = 1
Do Until k = ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.count + 1
'Select kth item in Override Set Name Slicer
ActiveWorkbook.SlicerCaches(SC3).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.SlicerItems(k).Name)
'Call sub that copies and pastes summary values
Call SelectedComboOnly
k = k + 1
Loop
j = j + 1
Loop
i = i + 1
Loop
我尝试用类似的东西替换 Do Until 语句:
Do Until i = ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItems.count + 1
但这会引发 运行 时间错误。我发现 here 那 "Attempting to access the VisibleSlicerItems property for slicers that are connected to an OLAP data source (SlicerCache.OLAP = True) generates a run-time error." 真的没有办法在这里访问可见切片器项目的数量吗?或者我应该尝试一些完全不同的东西吗?我的宏完成了它应该做的事情,但大约需要 25 分钟。我只是试图 运行 通过仅可见的切片器项目来优化代码。这将使它必须 运行 从 1,458 到 84 的组合。我已经关闭了屏幕更新。
尝试使用这个逻辑:
Sub Test()
Dim SC As SlicerCache
Dim SI As SlicerItem
For Each SC In ThisWorkbook.SlicerCaches 'this will loop through all your slicer caches in the whole workbook
'if you want to skip an slicer do it here and skip to the next
For Each SI In SC.VisibleSlicerItems 'this will loop through all your visible slicer items
'some code
Next SI
Next SC
End Sub
抱歉,回答晚了,但我只想分享以下对我有用的解决方案:
UBound(ThisWorkbook.SlicerCaches(SC1).SlicerCacheLevels(1).VisibleSlicerItemsList)
编辑:
.VisibleSlicerItemsList 属性 returns 一个数组,因此您不能像对集合那样在其上使用 .count 属性。为了获得数组的长度,使用函数 UBound (see here).