显式选项
Sub TestExample()
Dim MyArr() As Variant
MyArr = ArrayListOfSelectedAndVisibleSlicerItems("Slicer_A")
'now variable MyArr keeps all items in an array
End Sub
Public Function ArrayListOfSelectedAndVisibleSlicerItems(MySlicerName As String) As Variant
'This function returns an array of the limited set of items in Slicer A
'Limitation is due to both:
'(1) direct selection of items by user in slicer A
'(2) selection of items in slicer B which in consequence limits the number of items in slicer A
Dim ShortList() As Variant
Dim i As Integer: i = 0 'for iterate
Dim sC As SlicerCache
Dim sI As SlicerItem 'for iterate
Set sC = ThisWorkbook.Application.ActiveWorkbook.SlicerCaches(MySlicerName)
For Each sI In sC.SlicerItems
If sI.Selected = True And sI.HasData = True Then 'Here is the condition!!!
'Debug.Print sI.Name
ReDim Preserve ShortList(i)
ShortList(i) = sI.Value
i = i + 1
End If
Next sI
ArrayListOfSelectedAndVisibleSlicerItems = ShortList
End Function
最近才发现此代码,但该行出现错误
For Each sI In sC.SlicerItems
任何人都可以帮助解决这个问题,对我来说,变体类型的存储值是必须的 谢谢
答案 0 :(得分:0)
Paul,我要确保您的 MySlicerName 变量正确。尝试通过打印sC.SlicerItems.Count
进行测试,以查看其是否不为0。此外,从MS Docs网站-*尝试访问连接到外部OLAP数据源的切片器的SlicerItems属性(SlicerCache。OLAP = True)生成运行时错误。 *
在不知道实际错误的情况下很难说。