【问题标题】:How to select one slicer = true and remaining = false in VBA如何在 VBA 中选择一个切片器 = true 和剩余 = false
【发布时间】:2018-07-24 21:03:22
【问题描述】:

试图缩小这个 VBA 脚本的范围,而不必列出 .我是一个基本的宏用户。如何在不必列出所有 160 个的情况下选择一个 = True 而其余的 = False?

With ActiveWorkbook.SlicerCaches("Slicer_Organization")
    .SlicerItems("900110 - Danish Bemidji ").Selected = True
    .SlicerItems("900101 - Arabic Bemidji").Selected = False
    .SlicerItems("900105 - Chinese Callaway 1st Half").Selected = False
    .SlicerItems("900106 - Chinese Callaway 2nd Half").Selected = False
    .SlicerItems("900115 - Finnish Bemidji ").Selected = False
    .SlicerItems("900120 - French Bemidji 1st Half ").Selected = False
    .SlicerItems("900121 - French Bemidji 2nd Half ").Selected = False

【问题讨论】:

    标签: vba slicers


    【解决方案1】:

    过滤 SlicerItem 可能很棘手,因为至少有一个项目必须始终保持可见。

    这段代码展示了如何在一个名为 vSelection 的数组上过滤切片器。要修改您的需求,只需更改 vSelection = Array("B", "C", "E") 行,使其包含感兴趣的项目。

    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")
    
    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】:

      您可以使用For Each 循环遍历SlicerCache 中的所有SlicerItems

      Dim si as SlicerItem
      
      For Each si In ActiveWorkbook.SlicerCaches("Slicer_Organization").SlicerItems
          si.Selected = (si.Name = "900110 - Danish Bemidji")
      Next si
      

      (si.Name = "900110 - Danish Bemidji") 将返回一个TrueFalse 并设置SlicerItemSelected 属性,您正在适当地迭代。

      【讨论】:

      • 如何在每个数据切片之后保存文件(对用户隐藏所有其他切片器选项)......这样我想要一个名为“900110 - Danish Bemidji”的文件和下一个“900115 - Finnish Bemidji”等等?我需要每次都打开我的工作簿吗?
      • 那为什么要使用切片机呢?取而代之的是直接进入数据透视缓存和过滤器,创建一个新工作簿,复制,粘贴,用任何名称关闭新工作簿,冲洗并重复。切片器是我们人类的 UI,但如果您只是尝试以编程方式过滤数据透视缓存并捕获结果,那么开销很大。
      • @JNevill 如何管理您的解决方案以在您发布时选择项目但其他项目应设置为 false?
      【解决方案3】:
      Sub ExcelGuruVG()
        Dim myval As String
        myval = "VG"
        Dim sli as SlicerItem
      
        with Activeworkbook.slicercaches("Slicer_SLICERNAME")
          for Each sli in SlicerItems
            if sli.Name = myval then
              sli.select = true
            Else
              sli.selected = False
            End if
          Next sli
        End with
      End Sub
      

      【讨论】:

      • 能否请您为您的答案添加更多上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,它将帮助提问者和未来的读者。另请参阅Explaining entirely code-based answers
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多