Excel – Sort combobox values alphabetically

excelsortingvba

I have a combobox in a userform for excel. What is the easiest way to sort it alphabetically? The values for it are hardcoded in vba and new ones are just added to the bottom so they are not in any kind of order already.

The userform is currently being used so that our users can import data from our database into excel. The combobox is there so they can specify which client data to import.

Best Answer

As you are adding them, compare them to the values already in the combobox. If they are less than the item you come across, the replace the item. If they are not less than, then move on until you find something the item is less than. If it cannot find the item, then add it to the end.

For X = 0 To COMBOBOX.ListCount - 1
  COMBOBOX.ListIndex = X
  If NEWVALUE < COMBOBOX.Value Then
     COMBOBOX.AddItem (NEWVALUE), X
     GoTo SKIPHERE
     End If
Next X
        COMBOBOX.AddItem (NEWVALUE)
SKIPHERE: