VBA loop through slicer and export multiple sheets to PDF (withoud hidden slicer items)

tmoreira001

New Member
Joined
Jan 18, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hi, I am using the below VBA to export to pdf based on a slicer. The main problem is that it is selecting those slicers that are not visible (no data). How can I have a loop with only visible slicers? HELP PLEASE!
VBA Code:
Sub Reporte()
Dim sC As SlicerCache
Set sC = ActiveWorkbook.SlicerCaches("Slicer_X")

For i = 1 To sC.SlicerItems.Count

sC.SlicerItems(i).Selected = True
If i <> 1 Then sC.SlicerItems(i - 1).Selected = False
   
    ActiveSheet.Range("AA2") = sC.SlicerItems(i).Name

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\X\Desktop\" & "Reporte" & Range("AA2").Text & ".pdf", Quality:= _
        xlQualityHigh, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

Next


End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Assuming that your slicer is connected to a pivot table, try setting the MissingItemsLimit property of the PivotCache object to xlMissingItemsNone, and refreshing the pivot cache before looping through your slicer items, as follows . . .

VBA Code:
Sub Reporte()
Dim sC As SlicerCache
Set sC = ActiveWorkbook.SlicerCaches("Slicer_X")

With sC.PivotTables(1).PivotCache
    .MissingItemsLimit = xlMissingItemsNone
    .Refresh
End With

For i = 1 To sC.SlicerItems.Count

sC.SlicerItems(i).Selected = True
If i <> 1 Then sC.SlicerItems(i - 1).Selected = False
  
    ActiveSheet.Range("AA2") = sC.SlicerItems(i).Name

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\X\Desktop\" & "Reporte" & Range("AA2").Text & ".pdf", Quality:= _
        xlQualityHigh, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

Next


End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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