微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在引用之前清除特定切片器

如何解决在引用之前清除特定切片器

我正在寻找一种在下面的代码运行之前清除特定切片器的方法,而无需清除工作簿中的所有切片器。 target.address D11、D12 等在目录中(最终将有大约 40 个),但如果未清除所选切片器并且有人单击使用相同切片器的不同单元格,则它返回调试消息。我也愿意通过其他方式解决这个问题。

break;

解决方法

你总是需要一个可见的项目,所以在隐藏其他项目之前设置它 (未经测试,因此可能需要调整)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim addr As String,arrItems,arrCells,m,lbl,pi As PivotItem
    
    arrCells = Array("D11","D12","D13","D14","D15","D16") 'cells to be selected
    
    arrItems = Array("All Occasions - Main Meals and Between Meals",_
                     "Total Main Meals","Breakfast (Includes Brunch)",_
                     "Lunch","Dinner","Between Meal Occasions")
    
    If Target.Cells.CountLarge > 1 Then Exit Sub
    
    If Not Application.Intersect(Target,Me.Range("D11:D15")) Is Nothing Then
        
        addr = Target.Address(False,False)      'cell address
        m = Application.Match(addr,0) 'position in arrCells (1-based)
        lbl = arrItems(m - 1)                    'correspondint label (zero-based so adjust by -1)
        
        With Sheets("Trend In Meals").PivotTables("PivotTable3").PivotFields("Meal Occasion")
            .PivotItems(lbl).Visible = True 'set the visible one first
            For Each e In arrItems
                If e <> lbl Then .PivotItems(e) = False 'then hide the others
            Next e
        End With
        Sheets("Trend In Meals").Select
    End If
    
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。