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

后端 未结 2 1807
刺人心
刺人心 2021-01-15 15:25

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 \"S

相关标签:
2条回答
  • 2021-01-15 15:33

    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
    
    0 讨论(0)
  • 2021-01-15 15:42
    Sub FacultyToPDF()
    
    Dim wb As String
    Dim sh As Worksheet
    Dim fname As String
    Dim location As String
    Dim sI As SlicerItem, sI2 As SlicerItem, sC As SlicerCache
    Dim index As Integer
    Const PrintRange = "Print_Area"    
    
    fPath = "C:\Users\xiaz01\Desktop\Special Project\PDF"
    Set sC = ActiveWorkbook.SlicerCaches("Slicer_billing_phys_name")    
    
    
    For Each sI In ActiveWorkbook.SlicerCaches("Slicer_billing_phys_name").SlicerCacheLevels(1).SlicerItems
        ActiveWorkbook.SlicerCaches("Slicer_billing_phys_name").VisibleSlicerItemsList = Array(sI.Name)
        fname = Range("B1").Text & Format(Date, " yy-mm-dd") & ".pdf"
        Range(PrintRange).ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath & "\" & fname
    Next    
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题