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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,499
Messages
5,832,067
Members
430,109
Latest member
tinezi

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
Top