Populate a ComboBox on a form

hugs68

New Member
Joined
Dec 13, 2005
Messages
1
I need to populate a ComboBox in a VBA form with values from a column in a worksheet but I want to remove any duplicate values. The column contains a list of last names and I only want unique values to appear in the ComboBox.

The first names ComboBox on te form is then populated according to the last name selected. This part I have working but I'm stumped as to removing the duplicates!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
hi assuming name list start from A1 on Sheet1

if you set RowSource property or any other source regarding
data source on property box, clear all, otherwise error

Code:
Private Sub UserForm_Initialize()
Dim dic As Object, x, r As Range
Set dic = CreateObject("scripting.dictionary")
dic.comparemode = vbTextCompare
With Sheets("sheet1") ' <-- alter to suite
    For Each r In .Range("a1", .Range("a" & Rows.Count).End(xlUp))
        If Not IsEmpty(r) Then
            If Not dic.exists(r.Value) Then
                dic.Add r.Value, Nothing
            End If
        End If
    Next
End With
x = dic.keys: Set dic = Nothing
Me.ComboBox1.List = x
Erase x
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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