Slicer Loop Multi-Selecting

cc1987

New Member
Joined
Mar 4, 2016
Messages
18
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?
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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I've solved this myself, thanks.

Was an issue with the change event working before the loop was finished selectecting the next value
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top