How do I sort a combobox alphabetically?

DreyFox

Board Regular
Joined
Nov 25, 2020
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hello! I have a very simple combobox for which, I would like to sort alphabetically if possible. I was wondering if there is any VBA code that can achieve this?

Any help would be greatly appreciated. Thank you!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi & welcome to MrExcel.
How are you populating the combo?
 
Upvote 0
I found an online VBA code that populates it based on the checkboxes I pick. I found it on StackOverflow (I'd have to look it up again).
 
Upvote 0
I would need to see the code, or need to know how it should be populated.
 
Upvote 0
I would need to see the code, or need to know how it should be populated.
Ah okay, here's the code:
VBA Code:
im SearchString As String
Dim SearchRange As Range
Dim ans As String
Dim i As Long
If CheckBox37.Value = True Then
    SearchString = CheckBox37.Caption
    Set SearchRange = Sheets("Table of Part numbers").Range("A31:A38").Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
    If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
    ans = SearchRange.Offset(0, 1).Value
    ComboBox1.AddItem ans
End If

If CheckBox37.Value = False Then
    SearchString = CheckBox37.Caption
    Set SearchRange = Sheets("Table of Part numbers").Range("A31:A38").Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
    If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
    ans = SearchRange.Offset(0, 1).Value

For i = ComboBox1.ListCount - 1 To 0 Step -1
  If ComboBox1.List(i) = ans Then
    ComboBox1.RemoveItem (i)
  End If
Next
End If
 
Upvote 0
OK, try adding this to the end of that code
VBA Code:
   Dim Ary As Variant
   Dim i As Variant
   Ary = Me.ComboBox1.List
   If IsArray(Ary) Then
      With CreateObject("system.collections.arraylist")
         For i = LBound(Ary) To UBound(Ary)
            .Add Ary(i, 0)
         Next i
         .Sort
         Me.ComboBox1.List = .toarray
      End With
   End If
 
Upvote 0
Solution
OK, try adding this to the end of that code
VBA Code:
   Dim Ary As Variant
   Dim i As Variant
   Ary = Me.ComboBox1.List
   If IsArray(Ary) Then
      With CreateObject("system.collections.arraylist")
         For i = LBound(Ary) To UBound(Ary)
            .Add Ary(i, 0)
         Next i
         .Sort
         Me.ComboBox1.List = .toarray
      End With
   End If
Would I add this code at the end of that code for every checkbox?
 
Upvote 0
No it needs to go after all the checkboxes.
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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