Pivot Table/Slicers and VBA UserForm Combo box

gw92X

New Member
Joined
Jan 7, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am using the below code to populate my combo box using values from a slicer:

Private Sub UserForm_Initialize()
Dim SLSP As SlicerItem
Me.ComboBox1.Clear
For Each SLSP In Sheets("Sheet1").PivotTables("PivotTable3").Slicers("Firm Name").SlicerCache.SlicerItems
Me.ComboBox1.AddItem SLSP.Name
Next SLSP
End Sub

When I filter my pivot table, my slicer changes it's values and only shows the required values that have data as per the filter. (I've hidden values with no data due to the filter. If I unhide the values, they are sort of faded/greyed out in the slicer)

When I run the code, the combo box shows the filtered values first and then all other values that are hidden.

Is there any way I can get the combo box to only show the filtered values and not all the values?

I hope that makes sense. Sorry if it doesn't, I am new to VBA, pivot tables and slicers!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to the Board


Please test this:

VBA Code:
Private Sub UserForm_Initialize()
Dim SLSP As SlicerItem, pt As PivotTable
Set pt = Sheets("Sheet2").PivotTables("Pivot27")
Me.ComboBox1.Clear
For Each SLSP In pt.Slicers("CR Carrier").SlicerCache.SlicerItems
    If SLSP.Selected And SLSP.HasData Then Me.ComboBox1.AddItem SLSP.Name
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,060
Members
449,206
Latest member
Healthydogs

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