Listbox without duplicates in a VBA Excel userform
Solved
wire less
Posted messages
247
Registration date
Status
Member
Last intervention
-
f894009 Posted messages 17417 Registration date Status Member Last intervention -
f894009 Posted messages 17417 Registration date Status Member Last intervention -
Good classic topic... sorry I've browsed the forums but I still can't create my list box without duplicates!!
My script... I have a ComboBox1 and I want that when I click on it, I populate my list box. My list box fills up... without having the same item twice!!
So I did this:
There it is... simplistic... (yes well... it's what I found on the forums... it wasn't my idea... I'm adding the elements of my combobox into a "Collection", (apparently collections do not accept duplicates), then I transfer my collection into the list box.
And well... guess what... it doesn't work :'-(
Does anyone have an idea or a correction... or another solution?
Thanks.
My script... I have a ComboBox1 and I want that when I click on it, I populate my list box. My list box fills up... without having the same item twice!!
So I did this:
Private Sub ComboBox1_Click()
Dim Unique As New Collection
Dim Value As Range
Unique.Add ComboBox1.Value
For Each Value In Unique
Me.ListBox1.AddItem Value
Next Value
There it is... simplistic... (yes well... it's what I found on the forums... it wasn't my idea... I'm adding the elements of my combobox into a "Collection", (apparently collections do not accept duplicates), then I transfer my collection into the list box.
And well... guess what... it doesn't work :'-(
Does anyone have an idea or a correction... or another solution?
Thanks.
3 answers
Hello to both of you,
Dim Unique As New Collection Dim i As Integer Dim Value As Variant Private Sub ComboBox1_Click() On Error Resume Next Unique.Add Item:=ComboBox1.Text, Key:=CStr(ComboBox1.ListIndex) On Error GoTo 0 'clear listbox ListBox1.Clear 'Loop through the contents of the collection to populate the ListBox For Each Value In Unique Me.ListBox1.AddItem Value Next Value End Sub
I'm going to wait a bit longer, if no one has a solution, I will try your solution with loops that test each occurrence to see if they are duplicates :-/