Search code examples
excelvbaslicers

VBA to select each slicer item AND then save each selected slicer item as a pdf?


I've designed a dashboard consisting of a number of different pivot tables and pivot charts.

All of these pivot tables/charts are controlled by 1 slicer called "Slicer_Store".

There are about 800 different Stores to choose from in this slicer.

I need to save a pdf of EVERY store's dashboard. The process of manually selecting each slicer item, then saving the sheet as a pdf file, is extremely time consuming with 800+ stores, so I was hoping to automate the process via VBA.

Here's my code so far:

Public Sub myMacro()
Dim sI As SlicerItem, sI2 As SlicerItem, sC As SlicerCache
Set sC = ActiveWorkbook.SlicerCaches("Slicer_Store")
With sC

    For Each sI In sC.SlicerItems
        sC.ClearManualFilter
        For Each sI2 In sC.SlicerItems
            If sI.Name = sI2.Name Then sI2.Selected = True Else: sI2.Selected = False
        Next

        Debug.Print sI.Name
        'add export to PDF code here
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\TestUser\Desktop\testfolder" & Range("b1").Text  & ".pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    Next

End With
End Sub

The code does process all though slicer items, but the file is not being saved as a pdf. I need each file to be saved as the value in B2, so it would be Store1.pdf, Store2.pdf, Store3.pdf, etc.

Any help would be hugely appreciated. This is a big project at work and a lot of people are dependent on these pdf files..


Edited code:

This should work, but it takes forever to go over all of the slicer items (800+). Also, I need to make sure that it only prints the first page (print area) so the slicer itself won't be printed.

Public Sub myMacro()
Dim sI As SlicerItem, sI2 As SlicerItem, sC As SlicerCache
Dim ws As Worksheet
Set sC = ActiveWorkbook.SlicerCaches("Slicer_Store_Number")
Set ws = Sheet18
With sC

    For Each sI In sC.SlicerItems
        sC.ClearManualFilter
        For Each sI2 In sC.SlicerItems
            If sI.Name = sI2.Name Then sI2.Selected = True Else: sI2.Selected = False
        Next

       Debug.Print sI.Name
        'add export to PDF code here
      ws.PageSetup.PrintArea = ws.Range("A1:N34").Address

       ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\testuser\Desktop\testfolder" & Range("M1").Text & ".pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    Next

End With
End Sub

Solution

  • This actually resolve the issue but the approach you get towards 800+ item would take forever to be completed. See below for another solution which needs a little bit of collaboration from the user but it is much faster.

    Add this line before printing to PDF:

     Range("b1") = sI.Name
    

    This will write name of the store to the range so later you can use it as the name of your pdf file.

    Also, add a slash to the end of your path:

     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
              "C:\Users\TestUser\Desktop\testfolder\" & Range("b1").Text  & ".pdf", Quality:= _
    

    IF you want to only print first page, you can set the print area right before above lines or use this:

    ActiveSheet.PrintOut from:=1, To:=1
    

    UPDATE

    In this solution you need to make sure that first slicer item, and only that one is selected (So you should not clear manual filter). This is coded based on that. The original code goes over all of the slicer items each time, select one and deselect the others which causes an extremely high computational cost.

    Public Sub myMacro()
    Dim sC As SlicerCache
    Set sC = ActiveWorkbook.SlicerCaches("Slicer_Store_Number")
    
    
    
    
      '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 Only Select Store-Number 1"
          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 Sheet18.PageSetup
    
        .PrintArea = Sheet18.Range("A1:N34" & lastRow).Address
    
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    
        End With
    
        Sheet18.Range("M1") = sC.SlicerItems(i).Name
    
       'This prints to C directory, change the path as you wish
    
       Sheet18.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\" & Range("M1").Text & ".pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    Next
    
    End Sub