简体   繁体   English

切片器中仅选择一个切片器项目

[英]Select only a single slicer item in a slicer

I have a question regarding the slicers of a Pivot table. 我对数据透视表的切片器有疑问。 Tried several things but it is not working. 尝试了几件事,但是没有用。 I am really sorry if this question has been asked before, I did not find a solution until now. 如果以前曾问过这个问题,我真的很抱歉,直到现在我才找到解决方案。

So I want to dis-select all selected slicer items inside a certain pivot table and then select one specific which is stored inside a variable. 因此,我想取消选择某个数据透视表中所有选定的切片器项,然后选择一个存储在变量中的特定项。

Here is my last code: 这是我的最后一个代码:

Sub select_slicer()

Dim myval As String
myval = "Value1"

Dim sli As SlicerItem

ThisWorkbook.Sheets("mysheet").Activate
    For Each sli In ActiveWorkbook.SlicerCaches("Slicer_Sales")
        If sli.name = myval Then
         sli.Selected = True
        Else
         sli.Selected = False
        End If
Next sli

    End Sub

All help will be appreciated. 所有帮助将不胜感激。

Untested (works in one of my macros similar to yours): 未经测试(在与您相似的宏之一中起作用):

ThisWorkbook.Sheets("mysheet").Activate
With ActiveWorkbook.SlicerCaches("Slicer_Sales")
For Each sli In .SlicerItems
    If sli.Name = myval Then
     sli.Selected = True
    Else
     sli.Selected = False
End if
Next sli
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM