I have a loop which goes through options in a slicer and prints out a report for each one. Is there a way to add a stop or skip command if there is no data present and the slicer option is greyed out as shown in the image?
Code:
Code:
VBA Code:
'This VBA will loop through your Power Pivot slicer and print the results to PDF.
'To get it working change slicer name and storage location in below VBA.
Public Sub PrintToPDF()
Dim sC As SlicerCache
Set sC = ActiveWorkbook.SlicerCaches("Slicer_Player")
Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path
'This reminds the user to only select the first slicer item
If sC.VisibleSlicerItems.Count <> 1 Or sC.SlicerItems(1).Selected = False Then
MsgBox "Please Select First Athlete In Slicer"
Exit Sub
End If
For i = 1 To sC.SlicerItems.Count
'Do not clear ilter as it causes to select all of the items (sC.ClearManualFilter)
sC.SlicerItems(i).Selected = True
If i <> 1 Then sC.SlicerItems(i - 1).Selected = False
'Debug.Print sI.Name
'add export to PDF code here
With Sheet3.PageSetup
.PrintArea = Sheet3.Range("A1:Q289" & lastRow).Address
.FitToPagesWide = 1
.FitToPagesTall = 3
End With
Sheet3.Range("AA1") = sC.SlicerItems(i).Name
'This prints to C directory, change the path as you wish
Sheet3.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
folderPath & "/Reports/" & Sheet3.Range("AA1").Text & "_" & _
Format(Date, "dd mmm yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
End Sub