如何选择取决于VBA中另一个切片器的选定值的切片器项目? (使用OLAP多维数据集)

时间:2019-06-03 14:45:37

标签: excel vba olap-cube slicers

好的,VBA大师!新手又来了。使用OLAP多维数据集数据。

这一次,我坚持尝试根据先前从另一个组中选择的切片器项目自动选择切片器项目(从另一个切片器组)。

为了提供背景信息,我试图在2个不同的切片器下选择同一个人。为了使事情更具挑战性,切片器为人们使用不同的描述符-一个切片器使用“ Smith,Bob”,另一切片器使用“ 123C-Smith,Bob”。

这是我的代码:

Dim wb As Workbook
Dim slItem As SlicerItem
Dim slItem2 As SlicerItem
Dim sc3 As SlicerCache
Dim sc3L As SlicerCacheLevel
Dim sc4 As SlicerCache
Dim sc4L As SlicerCacheLevel

Set wb = ActiveWorkbook
Set sc3 = wb.SlicerCaches("Slicer_Primary_Account_List_Combo__BI")
Set sc4 = wb.SlicerCaches("Slicer_TM_Hierarchy")
Set sc3L = sc3.SlicerCacheLevels(1)
Set sc4L = sc4.SlicerCacheLevels(3)

sc3L.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData

' Select the first person within the Sales Cube slicer
' This selects each slicer item in the Sales Cube and iterates through them
For Each slItem In sc3L.SlicerItems
    If slItem.HasData Then   ''' This ensures the iteration is only on items with data
        sc3.ClearManualFilter
        sc3.VisibleSlicerItemsList = Array(slItem.Name)
    End If
Next

' Now ensure the same person is also selected within the BM Cube slicer
For Each slItem2 In sc4L.SlicerItems
    sc4.ClearManualFilter ''' CODE WORKS UP TO HERE
    If slItem2.Value = Mid(slItem.Value, 8, 30) Then   ''' I am trying to force the selection on the second slicer by looking for the name match. But this is NOT working. BREAKPOINT.
        slItem2.Selected = True
    End If
Next

我在断点处收到运行时错误1004。说这是应用程序定义或对象定义的错误。

我试图修复这段代码太久了-需要您的专业知识!

最终目标是:每次迭代我只需要选择一个人。而且我需要在两个切片机上选择相同的人。

然后走!

1 个答案:

答案 0 :(得分:0)

好的,我们尝试一下:

Option Explicit
Sub Main()

    Dim wb As Workbook
    Dim slItem As SlicerItem
    Dim sc3 As SlicerCache
    Dim sc3L As SlicerCacheLevel

    Set wb = ActiveWorkbook 'ThisWorkbook is better if this code is run on the same workbook
    Set sc3 = wb.SlicerCaches("Slicer_Primary_Account_List_Combo__BI")

    sc3L.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData

    ' Select the first person within the Sales Cube slicer
    ' This selects each slicer item in the Sales Cube and iterates through them
    For Each slItem In sc3L.SlicerItems
        If slItem.HasData Then   ''' This ensures the iteration is only on items with data
            sc3.ClearManualFilter
            sc3.VisibleSlicerItemsList = Array(slItem.Name)
            Testing CStr(slItem.Name)
        End If
    Next
End Sub
Sub Testing(SalesName As String)

    Dim slItem2 As SlicerItem
    Dim sc4 As SlicerCache
    Dim sc4L As SlicerCacheLevel
    Dim wb As Workbook
    Dim SalesSplit As Variant

    'with this you are getting an array with the number and name being SalesSplit(1) the name
    'So taking that SalesName =  "123C - Smith, Bob" then SalesSplit(0) will be "123C" and SalesSplit(1) will be "Smith, Bob"
    SalesSplit = Split(SalesName, " - ")


    Set wb = ThisWorkbook 'if the workbook is the same containing the code
    Set sc4 = wb.SlicerCaches("Slicer_TM_Hierarchy")
    Set sc3L = sc3.SlicerCacheLevels(1)
    Set sc4L = sc4.SlicerCacheLevels(3)

    For Each slItem2 In sc4L.SlicerItems
        sc4.ClearManualFilter ''' CODE WORKS UP TO HERE
        If slItem2.Name = SalesSplit(1) Then   ''' I am trying to force the selection on the second slicer by looking for the name match. But this is NOT working. BREAKPOINT.
            slItem2.Selected = True
        End If
    Next

    'Continue your code, when this sub ends it will loop through the next item on your sales cube

End Sub

2个子,第一个“主”将称为第二个“测试”。

步骤1选择一个包含销售多维数据集上的数据的项目。

步骤2调用测试过程,以传递来自sales多维数据集的选定名称。然后,在第二个多维数据集上选择相同的项目并执行任务。完成后,返回步骤1