2

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
TylerH
  • 20,799
  • 66
  • 75
  • 101
ranopano
  • 509
  • 2
  • 16
  • 32
  • Where do you get the error? (Where is highlighted?) – M-- Apr 21 '17 at 15:29
  • When I received the error in the past, no line was highlighted. That's why I was suspecting it might have to do with the memory. I reran the code and so far it's been running for about 20 minutes, but no files are being saved and the dashboard doesn't appear to be updating with the slicer selections. Would I be able to email you the workbook? – ranopano Apr 21 '17 at 15:49
  • Currently at work but you can do it if you don't have any private data. Prior to that, I assume that you change the path (i.e. `"C:\Users\testuser\Desktop\testfolder"`) to a directory that exists on your machine. I know it is obvious but sometimes I would forget to. – M-- Apr 21 '17 at 16:05
  • Try running `Step Into` running. by pressing "F8". – M-- Apr 21 '17 at 16:08
  • Check-out my website. And we are here for help, not making money. – M-- Apr 21 '17 at 16:23

2 Answers2

1

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
M--
  • 25,431
  • 8
  • 61
  • 93
  • Thank you! If I wanted to modify this code so that only the 1st page of the document is printed, how would I do that? – ranopano Apr 19 '17 at 22:43
  • Ok, I have my Dashboard set up on page 1 and my slicer on page 2 (that's why I only wanted to print the 1st page). However, when I run the code I receive "Error 1004 "Application-defined or Object-defined error". I'm not sure what's causing this... – ranopano Apr 21 '17 at 15:09
  • @Darren Just try explicitly define your print area (wherever it is, first page or last). Read here: http://stackoverflow.com/questions/17623445/set-printing-area-in-excel-2013-using-macro – M-- Apr 21 '17 at 15:18
  • Have a look at my updated code above. I'm pretty the VBA is incorrect as I am still receiving the error. The range of the print area i need to print is ("A1:N34") – ranopano Apr 21 '17 at 15:27
  • @Darren This works much faster than previous code. Check it out. – M-- Apr 21 '17 at 21:20
  • @Darren It is now tested and debugged. It takes 13 minutes and 25 second on my machine to run. It may vary based on your system configuration. – M-- Apr 21 '17 at 21:34
0
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
sirandy
  • 1,834
  • 5
  • 27
  • 32
  • Hi William, welcome to the site. What does this code do/how does it address the question in a novel way? Code-only answers may be useful but we really urge answers to contain an explanation to avoid people getting confused, or worse, running dangerous code. – TylerH Aug 07 '19 at 20:49
  • Hi William, welcome to the site (times two lol) don't forget to add tags to your posts so it's easier for people to know what your question is about. For example, if I was asking a python question on how to use json I would maybe add the tags "python" and "json". – Kyle Bridenstine Aug 07 '19 at 21:37
  • Hi Kyler Tyler, Thank you, I'm new to VBA. This code loops through slicer from Power pivot and print each slicer. I've tried M-M's code, but it stops at "If sC.VisibleSlicerItems.Count <> 1 Or sC.SlicerItems(1).Selected = False Then". I'm not sure if it's due to power pivot. At least, my code works for me – William Xia Aug 08 '19 at 13:17