所以我想从我的“部门”切片器中检索所选项目并将它们存储在一个字符串中。但是,当我测试这个时,我只得到最后一个项目,甚至没有一个被选中。这适用于使用Department中相同的项目设置DepartmentX切片器,但是我想仅将所选项目存储在字符串中。任何帮助将不胜感激
这是我到目前为止所拥有的
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim PT1 As PivotTable, PT2 As PivotTable
Dim slc1 As Slicer, slc2 As Slicer
Dim slcCache1 As SlicerCache, slcCache2 As SlicerCache
Dim slcItem1 As SlicerItem, slcItem2 As SlicerItem
Dim slicer_data As String
If Sh.Name = "GP YTD - Tooling (FYE 2016)" Then
Set PT1 = Sheets("Sheet1").PivotTables
("PivotTable2") '(1)
Set slc1 = PT1.Slicers("Department") '(1)
Set slcCache1 = slc1.SlicerCache
Set PT2 = Sheets("Sheet2").PivotTables
("PivotTable3") '(2)
Set slc2 = PT2.Slicers("DepartmentX") '(2)
Set slcCache2 = slc2.SlicerCache
slcCache2.ClearManualFilter
For Each slcItem1 In slcCache1.SlicerItems
slcCache2.SlicerItems(slcItem1.Name).Selected = slcItem1.Selected
slicer_data = slcItem1.Name
Application.EnableEvents = True
Next slcItem1
MsgBox "Selected" & slicer_data
End If
End Sub
答案 0 :(得分:2)
将最后一部分修改为:
For Each slcItem1 In slcCache1.SlicerItems
slcCache2.SlicerItems(slcItem1.Name).Selected = slcItem1.Selected
If slcItem1.Selected Then slicer_data = slicer_data & "," & slcItem1.Name
Application.EnableEvents = True
Next slcItem1
MsgBox "Selected" & Mid$(slicer_data, 2)