Helena Brown
New Member
Members
Forum Posts: 1
Member Since:
December 22, 2021
December 22, 2021
Offline
I have an excel file containing multiple charts showing sales, demand, etc. in 5 sheets. These charts are linked and filtered by a slicer "Product Name". This slicer is located in sheet "Supplier" .
I am trying to export these 5 sheets to PDF per product by having the code to loop through slicer. The error I got is
The code highlighted in yellow when Debug is
SC.VisibleSlicerItemsList = SL.SlicerItems(SlicerverdiIndex + 1).Name
Below is my full code. I have spent weeks to research and try different suggestions on internet but no luck. Could anyone please help?
Sub ExportPDF()Dim SC As SlicerCache Dim SL As SlicerCacheLevel Dim SI As SlicerItem Dim PrintRange As Range Dim MyRangeArray As Variant Dim i As Integer Dim FName As String Dim FPath As String Set sC = ActiveWorkbook.SlicerCaches("Slicer_Product_Name2") 'Add slicer name between " " Set SL = sC.SlicerCacheLevels(1) Set sI = SL.SlicerItems(1) 'Sets slicer item to a start value 'c(ounter) is set to 1, ready to begin c = 1 'Repeat the loop until the slicer doesnt have any data. "Do while c=1" is to kick it off in the first place Do While c = 1 Or sI.HasData = True 'This makes sure that SI is the correct slicer. Needed for corrent file name. For Each SI In SL.SlicerItems If SI.Selected = True Then SlicerverdiIndex = c Exit For End If Next SI 'Ensure that print only happens when the slicer has data If sI.HasData = True Then 'Define file path for printed file storage FPath = "C:\Users\..." FName = SI.SourceName 'Define WHAT to print and how to build file name 'List of Excel Ranges to export from' ThisWorkbook.Sheets(Array("Sales", "Demand", "Supplier", "Inventory", "Distributor")).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=FPath & "\" & FName & ".pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False 'PRINT CODE FINISHED End If 'Select next Value in slicer SC.VisibleSlicerItemsList = SL.SlicerItems(SlicerverdiIndex + 1).Name 'Adds 1 to the counter, will loop until end of slicer has been reached. c = c + 1 Loop End Sub
Catalin Bombea
Iasi, Romania
Member
Dashboards
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
Forum Posts: 1917
Member Since:
November 8, 2013
November 8, 2013
Offline
Hi Helena,
In this loop, I think it's best to exit when SlicerverdiIndex + 1 exceeds the SL.SlicerItems.Count, my guess is that you have reached the max number:
if SlicerverdiIndex + 1 > SL.SlicerItems.Count Then Exit Do SC.VisibleSlicerItemsList = SL.SlicerItems(SlicerverdiIndex + 1).Name
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Mynda Treacy, Morly Uttam
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
1 Guest(s)
Top Posters:
Catalin Bombea: 1917
SunnyKow: 1432
Anders Sehlstedt: 900
Purfleet: 414
Frans Visser: 346
David_Ng: 306
Hans Hallebeek: 286
lea cohen: 246
Jessica Stewart: 219
A.Maurizio: 216
Newest Members:
Jeffree Bunning
Jackie Memije
John Franzon
Darcie Clayton
trevor mir
Ruby White
Marisa Bradshaw
Eugene Augustine
MacNaughton Finance
Tanu Rai
Forum Stats:
Groups: 3
Forums: 24
Topics: 6872
Posts: 30119
Member Stats:
Guest Posters: 49
Members: 33667
Moderators: 2
Admins: 3
Administrators: Mynda Treacy, Philip Treacy, Jessica
Moderators: Velouria, Riny van Eekelen
© Simple:Press —