这就是我正在尝试做的事情:我希望在运行宏时为切片器中的每个项目(单独)保存我的 Excel 文件的新副本。问题是每次我运行它时,我的 For Loop 都会出现问题,这里的“对于 slBox.SlicerItems 中的每个 slItem”我不明白。调试结果
Option Explicit
Sub SavingData()
Dim Folder As Workbook
Dim NewFolderName As String
Dim ReportFolder As String
Dim slItem As SlicerItem
Dim slDummy As SlicerItem
Dim slBox As SlicerCache
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Folder = ActiveWorkbook
Set slBox = ActiveWorkbook.SlicerCaches("Slicer_Regroupement_SousR")
ReportFolder = Folder.Sheets("Variables").Range("FileName").Value
ActiveWorkbook.Worksheets("Pilotage").Activate
ChDir ReportFolder
'loop through each slicer item
For Each slItem In slBox.SlicerItems
'clear all items to start
slBox.ClearManualFilter
'test each item against itself
For Each slDummy In slBox.SlicerItems
'if the item equals the item in the first loop, then select it
'otherwise don't show it (thus showing 1 at a time between the nested loops)
If slItem.Name = slDummy.Name Then slDummy.Selected = True Else: slDummy.Selected = False
Next slDummy
NewFolderName = Folder.Sheets("Variables").Range("ReportName").Value
ActiveWorkbook.SaveAs Filename:=ReportFolder & "\" & NewFolderName _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Next slItem
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub