【问题标题】:VBA change Slicer item selectionVBA更改切片器项目选择
【发布时间】:2018-07-05 14:27:53
【问题描述】:

我是 VBA 的新手,需要学习如何自动更改切片器上的选定值。 我首先尝试了一个非常简单的代码,但我尝试了以下代码的所有可能变化,总是得到错误 1004,这次是“应用程序定义或对象定义错误”

Sub SlicerSelect()
    With ActiveWorkbook.SlicerCaches("Slicer_Time")
        .SlicerItems("2016").Selected = False
    End With
End Sub

有人有想法吗? Here 也是我的切片器及其设置的图像。

顺便说一句,当我使用 .ClearManualFilter 命令时它可以工作。

非常感谢!

这也是通过手动过滤我的项目的宏记录:

Sub Macro2()
' Macro2 Macro
ActiveWorkbook.SlicerCaches("Slicer_Time2").VisibleSlicerItemsList = Array( _
    "[Booking Period].[Time].[YEAR].&[2018]")
End Sub

【问题讨论】:

  • 请添加您的代码以供审核。另外,您要选择什么选项?所有切片器选项?最近一年?等
  • (对不起图片...这是我第一次在这里问代码问题!)
  • 'Sub SlicerSelect() With ActiveWorkbook.SlicerCaches("Slicer_Time") .SlicerItems("2016").Selected = False End With End Sub' 我的代码。我正在尝试选择“2016”选项,我以为您可以在第一张图片上看到所有这些内容,抱歉
  • 我仍在调查,我注意到在我的 Locals 中,一旦我将 SlicerCache 的值分配给一个变量,这个值就会出现 由它的 SlicerItems 引起,所以我猜问题来自我的 Slicer 或者可能是源文件...但是手动我已经用它做了很多枢轴并更改了值没有任何问题,所以我不太明白!跨度>
  • 您能否启动宏记录器,手动过滤项目,然后将生成的代码添加到您的原始问题中。这通常会突出问题。

标签: excel slicers vba


【解决方案1】:

您的问题是有两种不同类型的数据透视表:

  • 基于范围的数据透视表,使用您最初使用的代码类型 已发布,并且允许您一次传递一个单独的 PivotItems; 和
  • 基于数据模型(即 PowerPivot)或 OLAP 多维数据集的数据透视表, 使用完全不同的语法,您必须传入一个 数组显示所有你想看到的项目,使用更多 语法混乱。

【讨论】:

    【解决方案2】:

    过滤 SlicerItem 可能很棘手,因为至少有一个项目必须始终保持可见。这段代码展示了如何在一个名为 vSelection 的数组上过滤切片器,并将向您展示您需要如何完成此操作。

    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")
    'Set sc = slr.SlicerCache
    
    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
    

    【讨论】:

    • 我的意思是这段代码给了我同样的错误,感谢您的耐心等待
    • 啊,我从您的编辑中看到您的数据透视表是基于 PowerPivot 数据模型或 OLAP 多维数据集的。这需要一种不同的方法。我会尽快回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多