eric24g
New Member
- Joined
- Jun 2, 2022
- Messages
- 8
- Office Version
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- Platform
- 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
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