Excel 按名称而不是编号引用切片机

Excel 按名称而不是编号引用切片机,excel,vba,Excel,Vba,我有一些代码引用切片器: For Each item In wb.SlicerCaches("Segment").SlicerItems If item.Selected = True Then If Len(sSegment) > 0 Then sSegment = sSegment & "|" sSegment = sSegment & item.Caption End If Next item 但是我得到了无效的过程调

我有一些代码引用切片器:

For Each item In wb.SlicerCaches("Segment").SlicerItems
    If item.Selected = True Then
        If Len(sSegment) > 0 Then sSegment = sSegment & "|"
        sSegment = sSegment & item.Caption
    End If
Next item
但是我得到了无效的过程调用或参数。我见过很多例子都是用名字来引用它们,但都无法实现。如果我使用(1)、(3)等,然后添加一个切片器,它会打乱顺序,因此代码会被弄乱

如何按名称引用它们,我的最终目标是遍历选定的项目。

尝试:

For Each item In wb.SlicerCaches.Item("Segment").SlicerItems

您可能需要通过在切片器前面添加
切片器来引用切片器

例如,我在包含书籍信息的表中添加了一个
Authors
切片器,我可以使用以下代码引用它:

Debug.Print ActiveWorkbook.SlicerCaches("Slicer_Author").Slicers("Author").Caption
我之所以知道要添加
切片器
,是因为我右键单击切片器,然后选择
切片器设置…
并看到以下内容:

这似乎与切片机有关。真是愚蠢的运气,我碰巧看到了这一点,并想尝试一下