How to add multiple slicers to same worksheet using VBA

eric24g

New Member
Joined
Jun 2, 2022
Messages
8
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Platform
  1. Windows
Hello,

I am trying to add multiple slicers to the worksheet, but my code is only adding either one or the other. Each slicer has a different pivot table coming from different worksheets within the workbook. How could I get it so that when the below code executes both slicers are added to the same worksheet.

Note: Pivot table for slicer 1 is coming from worksheet 1 and pivot table for slicer 2 is coming from worksheet 2.

---slicer 1 code------

Sub CreatSlicer1()
Dim rng As Range
Dim sl As Slicer
Dim sc As SlicerCache
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ActiveWorkbook

Set ws = Worksheets("Sheet2")
Set sourceSheet = Worksheets("worksheet 1")

On Error Resume Next
Workbooks("MyFile.xlsx").Worksheets("Sheet2").Activate
wb.SlicerCaches(1).Delete
On Error GoTo 0

Workbooks("MyFile.xlsx").Worksheets("worksheet 1").Activate
Set sc = wb.SlicerCaches.Add2(ActiveSheet.ListObjects("Table5"), "Category")
Set sl = sc.Slicers.Add(ws, , "Category 1", "Category")

Set rng = Range("W2:AC2")
sl.Top = rng.Top
sl.Left = rng.Left
sl.Width = 150 'there are 72 points to an inch or 28.35 points to a centimeter
sl.Height = 120

End Sub

---slicer 2 code----

Sub CreatSlicer2()
Dim rng As Range
Dim sl As Slicer
Dim sc As SlicerCache
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ActiveWorkbook

Set ws = Worksheets("Sheet2")
Set sourceSheet = Worksheets("worksheet 2")

On Error Resume Next
Workbooks("MyFile.xlsx").Worksheets("Sheet2").Activate
wb.SlicerCaches(1).Delete
On Error GoTo 0

Workbooks("MyFile.xlsx").Worksheets("worksheet 2").Activate
Set sc = wb.SlicerCaches.Add2(ActiveSheet.ListObjects("Table1"), "Category")

Set sl = sc.Slicers.Add(ws, , "Category 3", "Category")

Set rng = Range("F21:F21")
sl.Top = rng.Top
sl.Left = rng.Left
sl.Width = 160 'there are 72 points to an inch or 28.35 points to a centimeter
sl.Height = 120
Workbooks("MyFile.xlsx").Worksheets("Sheet2").Activate

End Sub

--I call this file below to execute both slicers----

Sub CreatAllChartsSlicers()
Call CreatSlicer1
Call CreatSlicer2
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It's not very clear which workbook and worksheet you're adding the slicers to. You seem to be adding the first slicer to the already open workbook "MyFile.xlsx" in the sheet "worksheet 1", so I've tried to do that in the following macro:
VBA Code:
Public Sub Add_Slicer1()

    Dim wb As Workbook
    Dim sourceSheet As Worksheet
    Dim table As ListObject
    Dim sl As Slicer
    Dim sc As SlicerCache
    
    Set wb = Workbooks("MyFile.xlsx")
    Set sourceSheet = wb.Worksheets("worksheet 1")
    
    While wb.SlicerCaches.Count > 0
        wb.SlicerCaches(1).Delete
    Wend
    
    Set table = sourceSheet.ListObjects("Table5")
    Set sc = wb.SlicerCaches.Add2(table, "Category")
    Set sl = sc.Slicers.Add(sourceSheet, , "Category 1", "Category")
    
    With sourceSheet.Range("W2:AC2")
        sl.Top = .Top
        sl.Left = .Left
        sl.Width = 150
        sl.Height = 120
    End With
    
    wb.Save
    
End Sub
 
Upvote 0
Hey John, I created another post where I was having issues with getting the slicers to work with my charts. If you have any time, could you look at it and see what I'm doing wrong. The link is below. Thanks. @John_w

 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,544
Members
449,316
Latest member
sravya

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