Populate combo box with week-ending dates, use to filter pivot table

mib1019

Board Regular
Joined
Nov 9, 2017
Messages
66
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello all Excel Gurus!

I'm wanting to put a combo box on a worksheet that holds a pivot table of data organized by year, month and week ending dates, and use that combo box instead of a slicer or timeline.

Here's a picture of the table's first few rows:
1641333623378.png


My combo box should populate with the unique list from the table's Week-Ending Dates column. I'd also like it sorted newest to oldest.

Can you help me with code to populate the combo, then filter the pivot table by the result of the combo?

Thanks in advance!
MB
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
hopefully not for your Mac.
2 macros, the 1st is to populate the combobox (if sorting wasn't necessary, it'd be less complicated).
The 2nd depends on the layout of your pivottable, so difficult to predict.

VBA Code:
Sub Populate_Combo()
     Arr = Range("A2:A58").Value2                               'the range with your week ending days

     Set dict = CreateObject("scripting.dictionary")            'create a dictionary
     For i = 1 To UBound(Arr)                                   'loop through all days
          dict(Arr(i, 1)) = Array(100000# - Arr(i, 1), Format(Arr(i, 1), "mm/dd/yy"))     ' add to dictionary 100,000-datevalue and dateformat
     Next

     arr1 = Application.Sort(Application.Index(dict.items, 0, 0), 1)     'sort the items of the dictionary (=dates descending
     arr2 = Filter(Application.Transpose(Application.Index(arr1, 0, 2)), "a", 0, vbTextCompare)     'sort the items of the dictionary
     Blad1.ComboBox1.List = arr2
End Sub

Sub YourComboUpdate()
     With ActiveSheet.PivotTables(1).PivotFields("week ending")
          .ClearAllFilters
          .EnableMultiplePageItems = False
          .CurrentPage = Blad1.ComboBox1.Value
     End With
End Sub
 
Upvote 0
Solution
I'm going to try this. Can I reference the range of dates using its listobject identifiers. Want to make sure the populating takes in new data.
 
Upvote 0
lots of variants ...
VBA Code:
     Set Arr = Sheets("blad1").ListObjects("tabel1").ListColumns("week ending").DataBodyRange.Value2                               'the range with your week ending days
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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