Populate form control Drop Down Box with unique value

vck2017

New Member
Joined
Jan 13, 2017
Messages
8
hi, I am trying to populate a drop down box with a column of dates and only want to populate unique values. I have tried the following code but it doesn't work. Can anyone please assist?


Sub FilterUniqueData()​
Dim Lrow As Long, test As New Collection​
Dim Value As Variant, temp() As Variant​
ReDim temp(0)​

On Error Resume Next​
With ActiveSheet​
Lrow = .Range("C" & Rows.Count).End(xlUp).Row​
temp = .Range("C10:C" & Lrow).Value​
End With​

For Each Value In temp​
If Len(Value) > 0 Then test.Add Value, CStr(Value)​
Next Value​

ActiveSheet.Shapes("Drop Down 6").ControlFormat.RemoveAllItems​

For Each Value In test​
ActiveSheet.Shapes("Drop Down 6").ControlFormat.AddItem Value​
Next Value​

Set test = Nothing​

End Sub​
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think you need to check whether the value is already in the collection before adding it. Untested but perhaps something like this:

Code:
Sub FilterUniqueData()

Dim Lrow As Long, test As New Collection
Dim Value As Variant, temp() As Variant
Dim testVal As Variant
ReDim temp(0)

On Error Resume Next
With ActiveSheet
    Lrow = .Range("C" & Rows.Count).End(xlUp).Row
    temp = .Range("C10:C" & Lrow).Value
End With

For Each Value In temp
    If Len(Value) > 0 Then
        Err.Clear
        testVal = test.Item(CStr(Value))
        If Err.Number <> 0 Then test.Add Value, CStr(Value)
    End If
Next Value

ActiveSheet.Shapes("Drop Down 6").ControlFormat.RemoveAllItems
For Each Value In test
    ActiveSheet.Shapes("Drop Down 6").ControlFormat.AddItem Value
Next Value
Set test = Nothing

End Sub

WBD
 
Upvote 0
Thanks, but still can't make it work. Do I need to initialize the drop down box somehow? The range of data in column C consists of dates, could that cause a problem?
 
Upvote 0
So your dates start at C10 and run down the column? I set up a quick test and the code posted works on my sheet. What do you mean by "can't make it work"?

WBD
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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