Forum

how to fill two com...
 
Notifications
Clear all

how to fill two comboboxes without duplicate

6 Posts
2 Users
0 Reactions
177 Views
(@belle)
Posts: 6
Active Member
Topic starter
 

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 have. Only one combobox filled with data from column f without duplicate. Anyone can help me how to fill the second combobox?

Thank you

 

Private Sub UserForm_Initialize()
Dim d As Object, va, i As Long
Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare
va = Sheets("DB").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

 
Posted : 20/11/2018 12:41 am
(@debaser)
Posts: 836
Member Moderator
 

Simple option is to just to duplicate what you do for the first combobox, like this:

 

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

d.RemoveAll
va = Sheets("DB").Range("K2", Cells(Rows.Count, "K").End(xlUp))
For i = 1 To UBound(va, 1)
d(va(i, 1)) = ""
Next
ComboBox2.List = d.keys

End Sub

 

If the two ranges should have the same number of rows, I'd suggest using two dictionaries so you only have to loop once, while populating both dictionaries.

 
Posted : 20/11/2018 6:32 am
(@belle)
Posts: 6
Active Member
Topic starter
 

thanks for your reply. I have done as you suggest but it doesn't work. run time error 424 object required.

 
Posted : 22/11/2018 4:04 am
(@debaser)
Posts: 836
Member Moderator
 

On which line? You will need to amend Combobox2 in the code to whatever the name of the second combobox is of course.

 
Posted : 22/11/2018 4:35 am
(@belle)
Posts: 6
Active Member
Topic starter
 

I just realize the name of second combobox is not Combobox2 but Combobox3. It works now. Thank you so much Velouria. It helps a lot

 
Posted : 22/11/2018 11:24 pm
(@debaser)
Posts: 836
Member Moderator
 

You're welcome.

 
Posted : 23/11/2018 7:01 am
Share: