Excel VBA更改切片器项目选择

Excel VBA更改切片器项目选择,excel,slicers,vba,Excel,Slicers,Vba,我是VBA的新手,需要学习如何在切片机上自动更改选定的值。 我首先尝试了一个非常简单的错误,但是我尝试了下面代码的所有可能的变化,总是得到一个错误1004,这次是“应用程序定义的或对象定义的错误” 有人有主意吗?也是我的切片器及其设置的图像 顺便说一下,当我使用.ClearManualFilter命令时,它会工作 非常感谢 这里还有一个通过手动筛选我的项目的宏记录: Sub Macro2() ' Macro2 Macro ActiveWorkbook.SlicerCaches("Slicer_T

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

有人有主意吗?也是我的切片器及其设置的图像

顺便说一下,当我使用.ClearManualFilter命令时,它会工作

非常感谢

这里还有一个通过手动筛选我的项目的宏记录:

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

筛选切片可能很棘手,因为至少有一个项目必须始终可见。这段代码显示了如何在名为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
选项显式
子过滤器许可证()
作为切片机的暗淡单反
将sc变暗为切片机
Dim si As Ritem
我想我会坚持多久
作为变体的暗维它
作为变体的选举
设置sc=ActiveWorkbook.SlicerCaches(“切片器ID”)
'设置sc=slr.SlicerCache
vSelection=数组(“B”、“C”、“E”)
对于sc数据透视表中的每个pt
pt.ManualUpdate=True“在更改每个数据透视项后停止刷新数据透视表
下一个pt
与sc
'至少有一项必须始终在切片器中保持可见,因此请首先
'项目可见,在例程结束时,检查它是否确实*应该*可见
.SlicerItems(1).Selected=True
'隐藏尚未隐藏的任何其他项目。
请注意,检查状态要比更改状态快得多。
'因此,仅当每个项目尚未隐藏时才隐藏它
对于i=2到.ms.Count
如果.SlicerItems(i).Selected,则.SlicerItems(i).Selected=False
接下来我
'使感兴趣的数据透视项可见
在出现错误时,如果找不到其中一项,请“继续下一步”
对于VSE选举中的每个vItem
.SlicerItems(vItem).Selected=True
下一个维特姆
错误转到0
'隐藏第一个数据透视项,除非它是感兴趣的国家之一
出错时继续下一步
如果InStr(UCase(Join(vSelection,“|”)、UCase(.SlicerItems(1.Name))=0,则.SlicerItems(1.Selected=False
如果错误号为0,则
.ClearAllFilters
MsgBox标题:=“未找到任何项目”,提示:=“切片器中未找到任何所需项目,因此我已清除筛选器”
如果结束
错误转到0
以
对于sc数据透视表中的每个pt
pt.ManualUpdate=False
下一个pt
端接头

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

  • 基于范围的数据透视表,这些数据透视表使用最初使用的代码类型 已发布,并允许您一次传递一个单独的数据透视项; 及
  • 基于数据模型(即PowerPivot)或OLAP多维数据集的数据透视表, 使用完全不同的语法,您必须传入 数组,显示所有您希望可见的项,使用更多 语法混乱

请添加您的代码以供审阅。另外,您要选择什么选项?所有切片器选项?最近一年?等等(很抱歉出现这些图像…这是我第一次在这里问代码问题!)使用ActiveWorkbook.SlicerCaches(“切片器时间”).SlicerItems(“2016”).Selected=False End With End Sub”作为我的代码。我正在尝试选择选项“2016”,我想你可以在第一张图片上看到所有这些,Sorry我仍在调查中,我注意到在我的本地人中,只要我将SlicerCache的值分配给一个变量,这个变量就会显示其SlicerItems的值,所以我猜问题来自我的切片器或者源文件。。。但是我用它手动做了很多旋转,并且毫无问题地更改了值,所以我真的不明白!请启动宏录制器,手动筛选项目,然后将结果代码添加到原始问题中。这经常会突出问题。我的意思是,这段代码给了我同样的错误,感谢您的耐心。啊,我从您的编辑中看到,您的数据透视表基于PowerPivot数据模型或OLAP多维数据集。这需要一种不同的方法。我马上回答。
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