Add 3 items to combo box list

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I have this code that add unique list to my combo box. I would like to know how I can add three items that will show in the first three items in the combo box list

i.e.

Standard 3 item list that will show no matter what
mary
Tom
John

And then the list the code includes below the standard list


VBA Code:
Private Sub UpdateList()
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dim rCell As Range, x, y, z

cbName.Clear
x = Wks.Cells(Rows.Count, "A").End(xlUp).Row

If x > 6 Then
    For Each rCell In Wks.Range("A7:A" & x)
        If Not Dic.Exists(rCell.Value) Then
            Dic.Add rCell.Value, Nothing
        End If
    Next rCell
    cbName.List = Dic.Keys
    
    With cbName
        For x = LBound(.List) To UBound(.List)
            For y = x To UBound(.List)
                If .List(y, 0) < .List(x, 0) Then
                    z = .List(y, 0)
                    .List(y, 0) = .List(x, 0)
                    .List(x, 0) = z
                End If
            Next y
        Next x
    End With
End If
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
Private Sub UpdateList()
Dim Lst As Object: Set Lst = CreateObject("system.collections.arraylist")
Dim rCell As Range, x, y, z

cbName.Clear
x = Wks.Cells(Rows.Count, "A").End(xlUp).Row

If x > 6 Then
    For Each rCell In Wks.Range("A7:A" & x)
        If Not Lst.Contains(rCell.Value) Then
            Lst.Add rCell.Value
        End If
    Next rCell
    Lst.Sort
    cbName.List = Lst.ToArray
   
    With cbName
      .AddItem "Mary", 0
      .AddItem "Tom", 1
      .AddItem "John", 2
    End With
End If
End Sub
 
Upvote 0
I get an error...

: Set Lst = CreateObject("system.collections.arraylist")

1594725026650.png
 
Upvote 0
Another option
Rich (BB code):
Private Sub UpdateList()
    Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
    Dim rCell As Range, x, y, z
    cbName.Clear
    x = wks.Cells(Rows.Count, "A").End(xlUp).Row
    If x > 6 Then
        For Each rCell In wks.Range("A7:A" & x)
            If Not Dic.Exists("Z" & rCell.Value) Then
                Dic.Add "Z" & rCell.Value, Nothing
            End If
        Next rCell
        cbName.List = Dic.Keys
        With cbName
            For x = LBound(.List) To UBound(.List)
                For y = x To UBound(.List)
                    If .List(y, 0) < .List(x, 0) Then
                        z = .List(y, 0)
                        .List(y, 0) = .List(x, 0)
                        .List(x, 0) = z
                    End If
                Next y
            Next x
            .AddItem "Mary", 0
            .AddItem "Tom", 1
            .AddItem "John", 2
        End With
    End If
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Guys, I just got around to put in the code in and its inserting a space in the middle.

1594741033302.png
 
Upvote 0
One of the cells in column A is blank.
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,670
Members
449,248
Latest member
wayneho98

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