如何在VBA中选择一个限幅器= true和其余= false

时间:2018-07-24 21:03:23

标签: vba slicers

试图缩小此VBA脚本,而不必列出。我是Macro的基本用户。我该如何选择一个=真,其余=假,而不必列出所有160个?

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

3 个答案:

答案 0 :(得分:1)

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

此代码显示如何过滤名为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

答案 1 :(得分:0)

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

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并设置您要适当迭代的Selected的{​​{1}}属性。

答案 2 :(得分:0)

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