如何选择带有数组的多个切片器项目?

时间:2020-01-24 13:07:06

标签: arrays excel vba pivot slicers

我试图在切片器中为数据透视表选择多个项目。

我创建了一个包含所有应选择项目的数组。我的代码只能选择一项。

For cnt = UBound(Visible_Both_Years) To 0 Step -1

'filled array
MsgBox Visible_Both_Years(cnt)

'Loop through filter 
With k
    For Each l In .PivotItems
        Select Case l.Name
            Case Is = Visible_Both_Years(cnt)
                l.Visible = True
            Case Else
                l.Visible = False
        End Select
    Next

End With

我是VBA的新手。

1 个答案:

答案 0 :(得分:1)

无需遍历数组,请尝试...

'Loop through filter
With k
    .ClearAllFilters 'clear any existing filters
    For Each l In .PivotItems
        If IsError(Application.Match(l.Name, Visible_Both_Years, 0)) Then
            l.Visible = False
        End If
    Next
End With

希望这会有所帮助!

相关问题