Pivot Table Slicer [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have been using this code (look below) to create a Slicer for my Pivot Table, which is located at Worksheet_One.

The Silcer's name is "My_Slicer_Date"

It works well but I would like to use the same code to create the same Slicer but for another Pivot Table, which is basicaly same Pivot Table as the previous one, but with different data set and it is located at Worksheet_two.

So let's say I created Worksheet_One with Pivot Table and Slicer, all good here. Now I'm about to create Worksheet_Two with Pivot Table but I can't use the code for the Slicer unless I delete Worksheet_One.

Would it be possible to somehow use that code to create multiple Pivot Tables without having to delete any Worksheets and therefore Pivot Tables?

Thank you!

VBA Code:
Sub create_slicer1()

Dim i As SlicerCaches
Dim j As SLICERS
Dim k As SLICER

Set i = ActiveWorkbook.SlicerCaches
Set j = i.Add(ActiveSheet.PivotTables(1), "Date", "My_Slicer_Date").SLICERS
Set k = j.Add(ActiveSheet, , "My_Slicer_Date", "Date", 0, 0, 100, 195)

k.Top = 45
k.Left = 0

k.Style = "SlicerStyleLight4"

k.RowHeight = 15
k.ColumnWidth = 80

ActiveSheet.Shapes.Range(Array("My_Slicer_Date")).Select
Selection.Placement = xlFreeFloating

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Used this instead.

VBA Code:
Dim SLcache As SlicerCache
Dim SL As SLICER
Dim wb As Workbook
Set wb = ActiveWorkbook

Set SLcache = wb.SlicerCaches.Add2(pt, "Date", _
"Date_July")

Set SL = SLcache.SLICERS.Add( _
ws, , "Date_July", "Date")

SL.Top = 45
SL.Left = 0

SL.Style = "SlicerStyleLight4"

SL.RowHeight = 15
SL.ColumnWidth = 80

ActiveSheet.Shapes.Range(Array("Date_July")).Select
Selection.Placement = xlFreeFloating
 
Upvote 0
Solution

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

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