Create multiple slicers for pivot chart *simplify* [VBA]

Martin_H

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

I am using this part of code (see below) to create multiple slicers (only 2 are shown here) for my pivot chart.

I have been wondering if it is possible to simplify/merge the code.

Thank you for help.

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

'Slicer Date
Set SLcache = wb.SlicerCaches.Add2(pt, "Date", "Date_May")

Set SL = SLcache.SLICERS.Add(ws, , "Date_May", "Date", 0, 0, 100, 150)

With SL
.Top = 30: .Left = 100: .Style = "SlicerStyleLight4": .RowHeight = 15: .ColumnWidth = 80
End With


'Slicer Machine
Set SLcache = wb.SlicerCaches.Add2(pt, "Machine", "Machine_May")

Set SL = SLcache.SLICERS.Add(ws, , "Machine_May", "Machine", 0, 0, 100, 150)

With SL
.Top = 30: .Left = 216: .Style = "SlicerStyleLight1": .RowHeight = 15: .ColumnWidth = 80
End With
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Maybe something like this...

VBA Code:
Option Explicit

Sub test()

    Dim wb As Workbook
    Set wb = ActiveWorkbook
    
    Dim ws As Worksheet
    Set ws = wb.ActiveSheet
    
    Dim pt As PivotTable
    Set pt = ws.PivotTables("PivotTable1")
    
    Dim fieldNames As Variant
    fieldNames = Array("Date", "Machine")
    
    Dim slicerStyles As Variant
    slicerStyles = Array("SlicerStyleLight4", "SlicerStyleLight1")
    
    Dim nextLeftPos As Long
    Dim gap As Long
    Dim i As Long
    
    nextLeftPos = 100 'starting left position
    gap = 20 'gap between slicers
    For i = LBound(fieldNames) To UBound(fieldNames)
        
        Dim SLcache As SlicerCache
        Set SLcache = wb.SlicerCaches.Add2(pt, fieldNames(i), fieldNames(i) & "_May")
        
        Dim SL As Slicer
        Set SL = SLcache.Slicers.Add(ws, , fieldNames(i) & "_May", fieldNames(i), 0, 0, 100, 150)
        
        With SL
            .Left = nextLeftPos
            .Top = 30
            .Style = slicerStyles(i)
            .RowHeight = 15
            .ColumnWidth = 80
            nextLeftPos = nextLeftPos + .Width + gap
        End With
        
    Next i

End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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