Create a multiple pdf from a single sheet

thomas819

New Member
Joined
Nov 23, 2020
Messages
36
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I would like to create multiple pdf from the same sheet. Only difference will be a different selection of pivot slicer.

My idea is as follow:
After I select a new item from pivot slicer then I would like to create an first page of pdf document.
Then after I select another item from pivot slicer then I would like to create a second page of pdf document.
This process will be continue until I have 5 pages inside an pdf document.
Only then I need print out this pdf document.

Could you please help me?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
See if this works for you. The sheet containing the slicer and pivot table must be the active sheet and you need change the PDFfilename and slicer name or index near the top of the code.
VBA Code:
Public Sub Create_PDF_From_Slicer_Items()

    Dim currentSheet As Worksheet
    Dim slCache As SlicerCache
    Dim slItem As SlicerItem, slMatch As SlicerItem
    Dim PDFsheetNames As String, PDFfilename As String
    
    PDFfilename = ThisWorkbook.Path & "\All_Slicer_Items.pdf"    'Full path and file name of PDF file to be created
    
    'Set slCache = ThisWorkbook.SlicerCaches("The_Slicer_Name")  'Either: slicer's name (right-click Slicer -> Slicer Settings -> Name to use in formulas)
    Set slCache = ThisWorkbook.SlicerCaches(1)                   'Or: slicer's index number
    
    Set currentSheet = ActiveSheet
    PDFsheetNames = ""
        
    Application.ScreenUpdating = False
    
    'Loop through each slicer item
    
    For Each slItem In slCache.SlicerItems
        
        'Show all items
        
        slCache.ClearManualFilter
                        
        'Select the next slicer item to update the pivot table with that item
        
        For Each slMatch In slCache.SlicerItems
            If slItem.Name = slMatch.Name Then slMatch.Selected = True Else: slMatch.Selected = False
        Next
        
        'Create a temporary copy of the current sheet and add it to the list of sheets to be saved as a PDF
        
        With ThisWorkbook
            currentSheet.Copy After:=.Worksheets(.Worksheets.Count)
            PDFsheetNames = PDFsheetNames & "\" & .Worksheets(.Worksheets.Count).Name
        End With
        
    Next
    
    slCache.ClearManualFilter
        
    'Select all temporary sheets and save as a PDF
    
    Worksheets(Split(Mid(PDFsheetNames, 2), "\")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfilename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    'Delete all temporary sheets
    
    Application.DisplayAlerts = False
    Worksheets(Split(Mid(PDFsheetNames, 2), "\")).Delete
    Application.DisplayAlerts = True
    
    currentSheet.Activate
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Solution
See if this works for you. The sheet containing the slicer and pivot table must be the active sheet and you need change the PDFfilename and slicer name or index near the top of the code.
VBA Code:
Public Sub Create_PDF_From_Slicer_Items()

    Dim currentSheet As Worksheet
    Dim slCache As SlicerCache
    Dim slItem As SlicerItem, slMatch As SlicerItem
    Dim PDFsheetNames As String, PDFfilename As String
   
    PDFfilename = ThisWorkbook.Path & "\All_Slicer_Items.pdf"    'Full path and file name of PDF file to be created
   
    'Set slCache = ThisWorkbook.SlicerCaches("The_Slicer_Name")  'Either: slicer's name (right-click Slicer -> Slicer Settings -> Name to use in formulas)
    Set slCache = ThisWorkbook.SlicerCaches(1)                   'Or: slicer's index number
   
    Set currentSheet = ActiveSheet
    PDFsheetNames = ""
       
    Application.ScreenUpdating = False
   
    'Loop through each slicer item
   
    For Each slItem In slCache.SlicerItems
       
        'Show all items
       
        slCache.ClearManualFilter
                       
        'Select the next slicer item to update the pivot table with that item
       
        For Each slMatch In slCache.SlicerItems
            If slItem.Name = slMatch.Name Then slMatch.Selected = True Else: slMatch.Selected = False
        Next
       
        'Create a temporary copy of the current sheet and add it to the list of sheets to be saved as a PDF
       
        With ThisWorkbook
            currentSheet.Copy After:=.Worksheets(.Worksheets.Count)
            PDFsheetNames = PDFsheetNames & "\" & .Worksheets(.Worksheets.Count).Name
        End With
       
    Next
   
    slCache.ClearManualFilter
       
    'Select all temporary sheets and save as a PDF
   
    Worksheets(Split(Mid(PDFsheetNames, 2), "\")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfilename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
   
    'Delete all temporary sheets
   
    Application.DisplayAlerts = False
    Worksheets(Split(Mid(PDFsheetNames, 2), "\")).Delete
    Application.DisplayAlerts = True
   
    currentSheet.Activate
   
    Application.ScreenUpdating = True
   
    MsgBox "Done"
   
End Sub
Thank you John. You helped me a lot :)
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

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