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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If you are using an ActiveX ComboBox, then I think this will do what you want...
VBA Code:
Sub UpdateList()
  Dim X As Long, Data As Variant, Delimiter
  Delimiter = "|"
  Data = Range("A7", Cells(Rows.Count, "A").End(xlUp))
  With CreateObject("Scripting.Dictionary")
    For X = 1 To UBound(Data)
      .Item(Data(X, 1)) = 1
    Next
    cbName.List = Split(Join(Array("Mary", "Tom", "John"), Delimiter) & Delimiter & Join(.keys, Delimiter), Delimiter)
  End With
End Sub
 
Upvote 0
I thought you need to sort the values in the combo?
 
Upvote 0
Yep...I just added the other part of the code to it...

VBA Code:
Private Sub UpdateReason()
Dim X As Long, Data As Variant, Delimiter

Delimiter = "|"
Data = Range("S7:S" & Application.Max(7, Range("A" & Rows.Count).End(xlUp).Row))

With CreateObject("Scripting.Dictionary")
    For X = 1 To UBound(Data)
      .Item(Data(X, 1)) = 1
    Next
    cbReason.List = Split(Join(Array("Good Trade", "Bad Trade", "Confident", "Broke Rules"), Delimiter) & Delimiter & Join(.keys, Delimiter), Delimiter)
End With

With cbReason
    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
 
Upvote 0
Sorry, I missed the part about wanting the list sorted. Here is the subroutine I posted earlier with my method of sorting it before assigning it to the ComboBox...
VBA Code:
Private Sub UpdateReason()
    Dim X As Long, Data As Variant, Delimiter As String, Uniques() As String
    
    Delimiter = "|"
    Data = Range("S7:S" & Application.Max(7, Range("A" & Rows.Count).End(xlUp).Row))
    
    With CreateObject("Scripting.Dictionary")
        For X = 1 To UBound(Data)
            .Item(Data(X, 1)) = 1
        Next
        Uniques = Split(Join(Array("Good Trade", "Bad Trade", "Confident", "Broke Rules"), Delimiter) & Delimiter & Join(.Keys, Delimiter), Delimiter)
    End With
    
    With CreateObject("System.Collections.ArrayList")
        For X = 0 To UBound(Uniques)
            .Add Uniques(X)
        Next
        .Sort
        cbReason.List = .ToArray
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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