【问题标题】:Return Chosen Slicer Values as variant返回选择的切片器值作为变体
【发布时间】:2019-07-29 23:48:48
【问题描述】:

选项显式

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

任何人都可以帮助解决这个问题,变体类型的存储值对我来说是必须的 谢谢

【问题讨论】:

    标签: vba pivot-table variant slicers


    【解决方案1】:

    Paul,我想说确保您的 MySlicerName 变量是正确的。尝试通过打印 sC.SlicerItems.Count 来测试它是否不是 0。此外,从 MS Docs 站点 - *尝试访问连接到外部 OLAP 数据源 (SlicerCache.OLAP = True) 的切片器的 SlicerItems 属性会生成一个运行时错误。 *

    不知道实际错误很难说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多