【问题标题】:How to update slicer cache with VBA如何使用 VBA 更新切片器缓存
【发布时间】:2020-05-16 01:23:00
【问题描述】:

我正在使用 Excel VBA 根据用户选择在切片器上隐藏/显示元素。

我有以下代码:

 Private Sub removeFilterWithSlicer()

 Dim slicerCache As slicerCache

 Set slicerCache = ThisWorkbook.SlicerCaches("Slicer_Channel1")

 slicerCache.SlicerItems("A").Selected = False
 slicerCache.SlicerItems("B").Selected = False
 slicerCache.SlicerItems("C").Selected = False
 slicerCache.SlicerItems("D").Selected = False
 slicerCache.SlicerItems("E").Selected = False
 slicerCache.SlicerItems("F").Selected = False

End Sub

其中 A、B 等是切片器中元素的名称。我已经交叉检查了切片器缓存的名称(“Slicer_Channel1”)。问题是元素没有像他们应该的那样被取消选择。当我调试代码时,我发现每个元素都被一个一个地取消选择,但是当我到达过程结束时,即 End Sub,它们都恢复到处于选中状态。

任何指针?

【问题讨论】:

  • 你能详细说明你想要做什么吗?您希望代码清除切片器,还是希望它在一个特定的 SlicerItem 或其他东西上过滤切片器?
  • @jeffreyweir 本质上,我想使用此代码清除切片器并选择用户事件,但正如 jsotola 所建议的那样,即使使用鼠标,我也无法取消选择所有过滤器。仍然欢迎建议
  • 酷。这是可以做到的。但首先,您能否进一步详细说明用户如何与电子表格进行交互,以及您希望他们这样做时发生什么?知道为什么您不只是希望用户使用切片器本身,而不是以编程方式清除切片器,这将很方便。
  • @jeffreyweir 所以,我有一个由复选框控制的各种渠道。所以,基本上如果我想在我的分析中包含元素 A,我会检查元素 A,然后会启用一个文本框来输入一些值,处理后我需要确保只选择用户选择的元素在过滤器中,这反过来将控制我所有的数据透视图。现在我正在做类似这样的 slicerCache.SlicerItems("A").Selected = True when A 被选中等等
  • 好的。所以你有效地使用 CheckBoxes 作为切片器?在我们继续之前,是否有任何特定的设计原因为什么您不只使用切片器,并完全取消对复选框(和相关代码)的需要?您可以在单击时使用 Ctrl 键来选择切片器中的多个项目。 (抱歉,我的设计理念是在考虑基于代码的方法之前,始终首先考虑可以在 Excel 中本地完成的操作。)

标签: vba excel slicers


【解决方案1】:

这段代码展示了如何在一个名为 vSelection 的数组上过滤切片器。

Option Explicit

Sub FilterSlicer()
Dim slr As Slicer
Dim sc As SlicerCache
Dim si As SlicerItem
Dim i As Long
Dim vItem As Variant
Dim vSelection As Variant

Set sc = ActiveWorkbook.SlicerCaches("Slicer_ID")
'Set sc = slr.SlicerCache

vSelection = Array("B", "C", "E")

For Each pt In sc.PivotTables
    pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed
Next pt

With sc

    'At least one item must remain visible in the Slicer at all times, so make the first
    'item visible, and at the end of the routine, check if it actually  *should* be visible
    .SlicerItems(1).Selected = True

    'Hide any other items that aren't already hidden.
    'Note that it is far quicker to check the status than to change it.
    ' So only hide each item if it isn't already hidden
    For i = 2 To .SlicerItems.Count
        If .SlicerItems(i).Selected Then .SlicerItems(i).Selected = False
    Next i

    'Make the PivotItems of interest visible
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vSelection
        .SlicerItems(vItem).Selected = True
    Next vItem
    On Error GoTo 0

    'Hide the first PivotItem, unless it is one of the countries of interest
    On Error Resume Next
    If InStr(UCase(Join(vSelection, "|")), UCase(.SlicerItems(1).Name)) = 0 Then .SlicerItems(1).Selected = False
    If Err.Number <> 0 Then
        .ClearAllFilters
        MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Slicer, so I have cleared the filter"
    End If
    On Error GoTo 0
End With


For Each pt In sc.PivotTables
    pt.ManualUpdate = False
Next pt

End Sub

【讨论】:

    【解决方案2】:

    必须至少选择一个。用鼠标选择时的工作方式相同。你不能取消全选

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多