Page 1 of 1

VBA code to loop through a slicer printing each slicer item

Posted: Thu Apr 13, 2017 10:40 am
by Frank
I have been trying to write a VBA code to loop through a slicer but keep hetting an error 1004 Application-defined or object-defined error. I can get the print to PDF working, but unable to loop. below is my code. Appreciate any help.. thanks

Sub Step_Thru_SlicerItems2()
Dim slItem As SlicerItem
Dim i As Long

Application.ScreenUpdating = False
With ActiveWorkbook.SlicerCaches("Slicer_Shareholder_Name2")
'--deselect all items except the first
.SlicerItems(1).Selected = True
For Each slItem In .VisibleSlicerItems
If slItem.Name <> .SlicerItems(1).Name Then _
slItem.Selected = False
Next slItem
Call Saveaspdf
'--step through each item and run custom function
For i = 2 To .SlicerItems.Count
.SlicerItems(i).Selected = True
.SlicerItems(i - 1).Selected = False
Call Saveaspdf
Next i
End With
Application.ScreenUpdating = True
End Sub