Slicer

deadeye123

New Member
Joined
Dec 27, 2016
Messages
10
I have a slicer that has multiple values. the slicer controls multiple pivot tables. I need to select each value in the slicer and print a PDF. there are 100 values in the slicers and do it manually is a time consuming. how do I automate the process via macros or modules?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try the following macro...

Assumptions:

Code:
1) The workbook containing the slicer and pivot tables is the active workbook.

2) The worksheet containing your pivot tables is the active sheet.


Notes:

Code:
1) Change the name of the slicer where indicated in the code.

2) Change the path to the destination folder where indicated in the code.

3)  Each PDF file is named based on its respective slicer item name (ie. SlicerItemName.pdf).

4)  If a file by the same name already exists, it will be overwritten.


Here's the macro...

Code:
Option Explicit

Sub CreatePDFForEachSlicerItem()

    Const sSlicerName As String = "Region" [COLOR=#008000]'change the slicer name accordingly[/COLOR]
    
    Dim sDestFolder As String
    Dim Idx As Long

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    On Error GoTo ErrHandler
    
    sDestFolder = "C:\Users\Domenic\Desktop\" [COLOR=#008000]'change the path accordingly[/COLOR]
    If Len(Dir(sDestFolder, vbDirectory)) = 0 Then
        MsgBox sDestFolder & " does not exist.", vbInformation
        GoTo ExitTheSub
    End If
    
    If Right(sDestFolder, 1) <> "\" Then
        sDestFolder = sDestFolder & "\"
    End If

    With ActiveWorkbook.SlicerCaches("Slicer_" & sSlicerName)
        .ClearManualFilter
        With .SlicerItems
            For Idx = 1 To .Count
                If Idx > 1 Then
                    .Item(Idx).Selected = False
                End If
            Next Idx
            For Idx = 1 To .Count
                ActiveSheet.ExportAsFixedFormat xlTypePDF, sDestFolder & .Item(Idx).Caption & ".pdf"
                If Idx < .Count Then
                    .Item(Idx + 1).Selected = True
                    .Item(Idx).Selected = False
                End If
            Next Idx
        End With
        .ClearManualFilter
    End With
    
    MsgBox "Completed...", vbInformation
    
ExitTheSub:
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    Exit Sub
    
ErrHandler:
    MsgBox "Error " & Err.Number & ":  " & Err.Description, vbCritical, "Error"
    Resume ExitTheSub
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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