Forum

Automatically Creat...
 
Notifications
Clear all

Automatically Create Slicers

17 Posts
2 Users
0 Reactions
980 Views
(@swallack202)
Posts: 77
Estimable Member
Topic starter
 

By the way, this recorded macro works, but I don't want to use it. I'm trying not to do this much hardcoding of values. That's why my procedure is trying to be modestly clever about this. Also, if I run this twice, I get an error that the slicer cache already exists. 

wb.SlicerCaches.Add2( _
ws.PivotTables("PT_Velocity_Forecast"), _
"[Table_Velocity].[ART]").Slicers.Add _
ws, _
"[Table_Velocity].[ART].[ART]", _
"ART", _
"ART", _
169.5, 816, 144, 155.5

 
Posted : 01/07/2022 8:25 am
(@debaser)
Posts: 836
Member Moderator
 

The method seems quite temperamental about its argument types. I'd suggest:

 

Dim pf As PivotField
For Each pf In ptObject.RowFields

' Create slicer cache and slicer for selected PT fields
Select Case pf.Caption
Case "ART", "TEAM", "PI", "SPRINT", "VALID", "WARNING"

strSourceField = pf.CubeField.Name
strSlcrName = pf.Caption
strSlcrCaption = pf.Caption

Set rng = Range("Q6:T6")
lSpace = lSpace + 160

' The empty param is the OLAP hierarchy level, which is N/A

Set slcr = wb.SlicerCaches.Add2( _
Source:=CVar(ptObject), _
SourceField:=CVar(strSourceField)).Slicers.Add( _
SlicerDestination:=ws.Name, _
Name:=ws.Name & " " & pf.Caption, _
Caption:=pf.Caption, _
Top:=rng.Top, Left:=50, Width:=150, Height:=120)

'With slcr
' .NumberOfColumns = 3
' .RowHeight = 13
' .ColumnWidth = 70
'End With
Case Else
' do nothing

End Select
Next pf

End Sub

 

and for your reset code it looks like you might as well just loop through all the slicercaches and delete them (that should delete the associated slicers at the same time).

 
Posted : 02/07/2022 8:00 am
Page 2 / 2
Share: