VBA: How to fill two comboboxes without duplicate items

nabila

New Member
Joined
Nov 13, 2018
Messages
1
Hi, I have two comboboxes and want to fill with details from column f and column K from my database without duplicate items. The database will be updated daily. Basically this is what I've done but the comboboxes have no output when I run the program. Can somebody help me?



Private Sub UserForm_Click()
Dim i As Long
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("F:K"))
For a = 6 To 11
If Sheet1.Cells(1, a).Value = Me("Label" & a).Caption Then
Me("combobox" & a).AddItem Sheet1.Cells(i, a).Value
End If
Next a
Next i
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
in the example, I load all values into an indexed collection.
this prevents duplicates.

Code:
Private Sub UserForm_Click()
Dim i As Long, a As Long
Dim col As New Collection
Dim vItm


On Error Resume Next


  'collect all cells into collection (unique only)
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("F:K"))
   For a = 6 To 11
        If InStr(Sheet1.Cells(1, a).Value, "Label") > 0 Then
           vItm = Sheet1.Cells(i, a).Value
           col.Add vItm, vItm
        End If
   Next a
Next i


   'load the combo w unique values
For i = 1 To col.Count
   'Me("combobox" & a).AddItem col(i)
   Debug.Print col(i)
Next

set col = nothing
End Sub
 
Last edited:
Upvote 0
The code should go in the UserForm_Initialize event.
 
Upvote 0
Hi, I have two comboboxes and want to fill with details from column f and column K from my database without duplicate items. The database will be updated daily. Basically this is what I've done but the comboboxes have no output when I run the program. Can somebody help me?

Here's another example on how to populate unique values from a range to a combobox.

Code:
Private Sub UserForm_Initialize()
Dim d As Object, va, i As Long
Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare
va = Sheets("Sheet1").Range("F2", Cells(Rows.Count, "F").End(xlUp))
    For i = 1 To UBound(va, 1)
        d(va(i, 1)) = ""
    Next
ComboBox1.List = d.keys

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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