选择切片器中的项目列表

时间:2015-11-26 10:49:43

标签: excel vba

我正在尝试通过其圣诞销售来帮助一家书店!我使用连接到OLAP多维数据集的数据透视表,其中包含大量产品参考,包括上周销售,库存水平等有价值的信息。

我想仅显示当前商业操作(约400本书)的图书数据(销售和库存水平),以检查库存水平是否足够。

我有一个带有ISBN编号的切片器,有超过一百万个字幕,我想用VBA操作那个切片,只显示我想要的书。

我想要显示的ISBN列表在“目录EOY”第3列中。我尝试使用正确的切片器名称构建一个数组,与VisibleSlicerItemsList语句一起使用,但是我收到一条消息“对象需要“在那一行(最后一行)。在我的例子中,我将书籍列表限制在前50个项目中。

知道如何解决这个问题吗?

Sub ShowProductList()
Dim ProductList(0 To 50) As Variant
Dim i
Dim Sc As SlicerCache
Dim sL As SlicerCacheLevel

Set Sc = ActiveWorkbook.SlicerCaches("Slicer_ISBN")
Set sL = Sc.SlicerCacheLevels(1)

For i = 2 To 52
    ProductList(i - 2) = Chr(34) & "[DIM Artikel].[ISBN].&[" & _
    Worksheets("Catalogue EOY").Cells(i, 3).Value & "]" & Chr(34)
Next i

sL.VisibleSlicerItemsList = ProductList

End Sub

2 个答案:

答案 0 :(得分:1)

Sub f()
Dim piv As PivotItem, pivf As PivotField, pivt As PivotTable, ProductList() As Variant, filterrng As Range, rng As Range
'the range where your background data is
Set filterrng = Worksheets("filter_criteria").Range("C2:C52") 'the range where your product list is
ReDim ProductList(filterrng.Cells.Count - 1)
For Each rng In filterrng
    ProductList(i) = rng.Value2
    i = i + 1
Next rng

Set pivt = Sheets("piv").PivotTables("PivotTable1") 'your pivottable, define it properly
Set pivf = pivt.PivotFields("ISBN") 'the pivot field
On Error Resume Next
For Each pvi In pivf.PivotItems
    pvi.Visible = False
    pvi.Visible = Application.Match(pvi.Name, ProductList, False) > -1 'if it's in the range, then make it visible, otherwise hide it
Next pvi
End Sub

不是你想要的答案,而是你需要的答案。

答案 1 :(得分:0)

你需要循环遍历每个SlicerItem并根据你的列表进行测试以选择是否选择它,具体如下:

Sub ShowProductList()
With Application
    .EnableEvents = False 'stop executing this code until we are done
    .DisplayAlerts = False
    .ScreenUpdating = False
    '.Calculation = xlCalculationManual
End With

Dim ProductList(0 To 50) As Variant
Dim i As Long
Dim Sc As SlicerCache
Dim sI As SlicerItem
Dim sL As SlicerCacheLevel
Dim inLisT As Boolean

Set Sc = ActiveWorkbook.SlicerCaches("Slicer_ISBN")
Set sL = Sc.SlicerCacheLevels(1)

For i = 2 To 52
    ProductList(i - 2) = Chr(34) & "[DIM Artikel].[ISBN].&[" & _
    Worksheets("Catalogue EOY").Cells(i, 3).Value & "]" & Chr(34)
Next i

Sc.ClearManualFilter
For Each sI In Sc.SlicerItems
    inLisT = False
    For i = LBound(ProductList) To UBound(ProductList)
        If sI.Name <> ProductList(i) Then
        Else
            inLisT = False
            Exit For
        End If
    Next i
    If inLisT Then
        sI.Selected = True
    Else
        sI.Selected = False
    End If
Next sI

With Application
    .EnableEvents = True 'stop executing this code until we are done
    .DisplayAlerts = True
    .ScreenUpdating = True
    '.Calculation = xlCalculationAutomatic
End With
End Sub