Slicer Object - adjust size based on content

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
727
Office Version
  1. 2016
Platform
  1. Windows
Greetings, is it possible to make a slicer object size based on the number of selections available?

I have this -

VBA Code:
Sub SLICER()
    ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.ListObjects("Table1"), "Area").Slicers.add ActiveSheet, , "Area", "Area", 1, 600, 150, 150
    With ActiveWorkbook.SlicerCaches("Slicer_Area").Slicers("Area")
        .Caption = "Area"
        .DisplayHeader = True
        .SlicerCache.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
        .SlicerCache.SortItems = xlSlicerSortAscending
        .SlicerCache.SortUsingCustomLists = True
    End With
End Sub

I was hoping I could set the size to automatically adjust based on the content, like "Area", "Area", 1, 600, , , - basically not declaring values for the width and height.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Actually, since the width and height parameters are optional, you can simply omit them like this . . .

VBA Code:
.Slicers.Add ActiveSheet, , "Area", "Area", 1, 600

Hope this helps!
 
Upvote 0
By the way, your macro can be re-written as follows . . .

VBA Code:
Sub SLICER()

    Dim sc As SlicerCache
    Set sc = ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.ListObjects("Table1"), "Area")
    
    Dim sl As SLICER
    Set sl = sc.Slicers.Add(ActiveSheet, , "Area", "Area", 1, 600)
    
    With sl
        .Caption = "Area"
        .DisplayHeader = True
        .SlicerCache.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
        .SlicerCache.SortItems = xlSlicerSortAscending
        .SlicerCache.SortUsingCustomLists = True
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Actually, since the width and height parameters are optional, you can simply omit them like this . . .

VBA Code:
.Slicers.Add ActiveSheet, , "Area", "Area", 1, 600

Hope this helps!
Thank you, when I omit the height I was hoping by default the height would show all options without the slider on the right.
 
Upvote 0
Oh I see, in that case, try something like this (you might have to play with it a bit to find the right settings) . . .

VBA Code:
.Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)

If you are going to the code I suggested earlier . . .

VBA Code:
Sub SLICER()

    Dim sc As SlicerCache
    Set sc = ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.ListObjects("Table1"), "Area")
    
    Dim sl As SLICER
    Set sl = sc.Slicers.Add(ActiveSheet, , "Area", "Area", 1, 600)
    
    With sl
        .Caption = "Area"
        .DisplayHeader = True
        .SlicerCache.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
        .SlicerCache.SortItems = xlSlicerSortAscending
        .SlicerCache.SortUsingCustomLists = True
        .Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Oh I see, in that case, try something like this (you might have to play with it a bit to find the right settings) . . .

VBA Code:
.Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)

If you are going to the code I suggested earlier . . .

VBA Code:
Sub SLICER()

    Dim sc As SlicerCache
    Set sc = ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.ListObjects("Table1"), "Area")
   
    Dim sl As SLICER
    Set sl = sc.Slicers.Add(ActiveSheet, , "Area", "Area", 1, 600)
   
    With sl
        .Caption = "Area"
        .DisplayHeader = True
        .SlicerCache.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
        .SlicerCache.SortItems = xlSlicerSortAscending
        .SlicerCache.SortUsingCustomLists = True
        .Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)
    End With
   
End Sub

Hope this helps!
This worked, I did have to change .RowHeight + 2 to +3, but it worked. Thank you.

Can you help explain what the last line is doing? .Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)
 
Upvote 0
This worked, I did have to change .RowHeight + 2 to +3, but it worked. Thank you.
That's great, glad it worked.
Can you help explain what the last line is doing? .Height = 30 + ((.RowHeight + 2) * .SlicerCache.SlicerItems.Count)
  • 30 is to allow extra space for the header
  • ((.RowHeight + 2) is the height of a slicer item, plus 2 for some padding on either side of it
  • .SlicerCache.SlicerItems.Count is a count of the number of slicer items in the slicer
Cheers!
 
Upvote 0
That's great, glad it worked.

  • 30 is to allow extra space for the header
  • ((.RowHeight + 2) is the height of a slicer item, plus 2 for some padding on either side of it
  • .SlicerCache.SlicerItems.Count is a count of the number of slicer items in the slicer
Cheers!
Genius!

Many thanks.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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