Combo Box List

jenrique.cerda

Board Regular
Joined
Sep 22, 2006
Messages
53
Hi everyone,

I have a problem regarding completing a list on a combo box.

I have this column on a sheet with several values. Some of them repeat themselves and some of them are blanks.

My question is, is there a way to fill my combo list without repeating values?

I want this to be like the autofilter, it never repeats values. Please this is urgent, appreciate your help.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This will also sort the list (assuming it's on a UserForm):

Code:
Private Sub UserForm_Initialize()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim Cell As Range
    Dim Coll As New Collection
    Dim Item As Variant
    Dim i As Long
    Dim j As Long
    Dim Temp As Variant
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A1:A" & Sh.Range("A" & Sh.Rows.Count).End(xlUp).Row)
    ComboBox1.Clear
    On Error Resume Next
    For Each Cell In Rng
        Coll.Add Cell.Value, CStr(Cell.Value)
    Next Cell
    On Error GoTo 0
    For Each Item In Coll
        ComboBox1.AddItem Item
    Next Item
    With ComboBox1
        For i = 0 To .ListCount - 1
            For j = i + 1 To .ListCount - 1
                If .List(i) > .List(j) Then
                    Temp = .List(j)
                    .List(j) = .List(i)
                    .List(i) = Temp
                End If
            Next j
        Next i
    End With
End Sub
 
Upvote 0
Still not working...
What about if this combo box name is CD and is on a UserForm named UserForm....

This userform pops-up when I click a button. I just want that whenever my userform (with the combobox) pops up, the combo box is sorted....

Please and thanks.
 
Upvote 0
try
Paste the code onto Form module
Code:
Private Sub UserForm_Initialize()
Dim dic As Object, e
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = vbTExtCompare
With Sheets("Sheet1") '<- change to suite
    With .Range("a1",.Range("a" & Rows.Count).End(xlUp))
        .Sort .Range("a1"), xlAscending
        a = .Value
    End With
End With
For Each e In a
    If Not IsEmpty(e) And Not dic.exists(e) Then dic.add e, Nothing
Next
Me.CD.List = dic.keys
Set dic = Nothing : Erase a
End Sub
 
Upvote 0
Do you have the ComboBox's RowSource set? If so unset it - you can't have that and set the List property.
 
Upvote 0
Hi jindon,

I`m afraid I get a "Permission denied" error..... any ideas???

thnks.

Hi
try

1) start Internet Explorer
2) go to [Tools] - [Internet Option] - selct [Security] tab
3) [Custom] - [Settings]
4) change "Disable" To "Enable", under [Initilize and script ActiveX controls not marked as safe] option

HTH
 
Upvote 0
i used andrews code and it worked perfect. But i was wondering, i have 2 combo boxes, what could i add to his code so that the item selected in the first combo box would not appear in the second combo box. Any ideas of how i should go about doing this?


Cheers
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,268
Members
449,149
Latest member
mwdbActuary

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