I have the following code which loops through a slicer and exports a pdf report for each slicer value. I've used it previously without issue however in my current workbook it always multi-selects rather than single. I have a worksheet module running on the same sheet which detects when a cell value changes and filters a different pivot based on the change. I think this may be causing the issue but not sure?
I have seen suggestions about using DoEvents to put a pause and allow the worksheet module to work bu tnor sure if it is suitable here?
I have seen suggestions about using DoEvents to put a pause and allow the worksheet module to work bu tnor sure if it is suitable here?
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_Player1")
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
DoEvents
'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