Sorting a Combobox A-Z?

termeric

Active Member
Joined
Jun 21, 2005
Messages
280
Hello, is there a way to sort a combo alphabetically? I'm not sure if this needs to be done before the box is populated or after.

Right now, I am building a list of unique values in ComboBox2 based on the selection of ComboBox1. The last thing I add is an "All Markets" option, which ends up showing up at the end. I'd like to alphabetize both so that "All Markets" is the first thing on the list, and so that the location on the list are in order instead of just as they occur in the source list.

Code:
Private Sub combobox1_Change()
Dim rngCompany As Range
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
Dim resultIndex As Integer
Dim i As Long
Dim strFound As Boolean
Dim myString As String

        
        resultIndex = -1
        
        ComboBox2.Clear
   
        ' check that a company has been selected
    If ComboBox1.ListIndex <> -1 Then
    
         strSelected = ComboBox1.Value
        
         LastRow = Worksheets("NB-LastYear").Range("E" & Rows.Count).End(xlUp).Row
        
         Set rngList = Worksheets("NB-LastYear").Range("E2:E" & LastRow)
    
          
    
    
        For Each rngCompany In rngList
        
             If rngCompany.Value = strSelected Then
            
                myString = rngCompany.Offset(, 2)
            
                strFound = False
                
                With Me.ComboBox2
                    '.AddItem ("All Markets")
                     'Loop through combobox
                    For i = 0 To .ListCount - 1
                        If .List(i) = myString Then
                            strFound = True
                            Exit For
                        End If
                    Next i
                     'Check if we should add item
                    If Not strFound Then .AddItem rngCompany.Offset(, 2)
                End With
            
                    'ComboBox2.AddItem rngCompany.Offset(, 2)

            End If
        
        Next rngCompany
        
    
    
        Me.ComboBox2.AddItem ("All Markets")

        Me.ComboBox2.Value = "All Markets"
    
    End If
 
End Sub
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about

Rich (BB code):
Private Sub combobox1_Change()
Dim rngCompany As Range
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
Dim resultIndex As Integer
Dim i As Long
Dim strFound As Boolean
Dim myString As String
Dim Lst As Object

Set Lst = CreateObject("system.collections.arraylist")
        
        resultIndex = -1
        
        ComboBox2.Clear
   
        ' check that a company has been selected
    If ComboBox1.ListIndex <> -1 Then
    
         strSelected = ComboBox1.Value
        
         LastRow = Worksheets("NB-LastYear").Range("E" & Rows.count).End(xlUp).Row
        
         Set rngList = Worksheets("NB-LastYear").Range("E2:E" & LastRow)
    
          
    
    
        For Each rngCompany In rngList
        
             If rngCompany.Value = strSelected Then
            
                myString = rngCompany.Offset(, 2)
            
                strFound = False
                
                With Me.ComboBox2
                    '.AddItem ("All Markets")
                     'Loop through combobox
                    For i = 0 To .ListCount - 1
                        If .List(i) = myString Then
                            strFound = True
                            Exit For
                        End If
                    Next i
                     'Check if we should add item
                    If Not strFound Then Lst.Add CStr(rngCompany.Offset(, 2))
                End With
            
                    'ComboBox2.AddItem rngCompany.Offset(, 2)

            End If
        
        Next rngCompany
        
    
    
       Lst.Add "All Markets"
       Lst.Sort
       Me.ComboBox2.List = Lst.toarray
        Me.ComboBox2.Value = "All Markets"
    
    End If
 
End Sub
 
Upvote 0
That does sort the values into alphabetical order, but it no longer adds only unique records into the combo box. I have a column of data where its 5-10 unique locations repeated a few hundred times each which is populating combobox2. should I move the sorting, or the adding of the "All Markets" u[p earlier in the code?
 
Upvote 0
How about
Rich (BB code):
Private Sub combobox1_Change()
Dim rngCompany As Range
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
Dim resultIndex As Integer
Dim i As Long
Dim strFound As Boolean
Dim myString As String
Dim Lst As Object

Set Lst = CreateObject("system.collections.arraylist")
        
        resultIndex = -1
        
        ComboBox2.Clear
   
        ' check that a company has been selected
    If ComboBox1.ListIndex <> -1 Then
    
         strSelected = ComboBox1.Value
        
         LastRow = Worksheets("NB-LastYear").Range("E" & Rows.count).End(xlUp).Row
        
         Set rngList = Worksheets("NB-LastYear").Range("E2:E" & LastRow)
    
          
    
    
        For Each rngCompany In rngList
        
             If rngCompany.Value = strSelected Then
            
                   If Not Lst.contains(CStr(rngCompany.Offset(, 2))) Then Lst.Add CStr(rngCompany.Offset(, 2))

            End If
        
        Next rngCompany
        
    
    
       Lst.Add "All Markets"
       Lst.Sort
       Me.ComboBox2.List = Lst.toarray
       Me.ComboBox2.Value = "All Markets"
    
    End If
 
End Sub
 
Upvote 0
My pleasure & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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