Tri d'un combobox par ordre alphabétique

LANGAZOU Messages postés 100 Statut Membre -  
 greg -
Bonjour,

je voudrais trier par ordre alphabétique ma combobox. voici mon code:

Private Sub UserForm_Initialize()
For i = 3 To Sheets("CD").Cells(3, 2).End(xlDown).Row
Me.ComboBox2 = Sheets("CD").Cells(i, 2)
If Me.ComboBox2.ListIndex = -1 Then
Me.ComboBox2.AddItem Sheets("CD").Cells(i, 2)
End If
Next i

End sub


Merci pour votre réponse

2 réponses

  1. Mytå Messages postés 4246 Date d'inscription   Statut Contributeur Dernière intervention   957
     
    Salut le Forum

    Sub TriAlpha_ComboBox()
    Dim i As Integer, j As Integer
    Dim strTemp As String

    'Supprime le contenu du ComboBox
    Feuil1.ComboBox1.Clear
    'Alimente le ComboBox
    Feuil1.ComboBox1.List() = Array("mimi", "nono", "bibi", "fifi", "lolo")

    'Tri le contenu du ComboBox par ordre alphabétique
    With Feuil1.ComboBox1
    For i = 0 To .ListCount - 1
    For j = 0 To .ListCount - 1
    If .List(i) < .List(j) Then
    strTemp = .List(i)
    .List(i) = .List(j)
    .List(j) = strTemp
    End If
    Next j
    Next i
    End With
    End Sub

    Mytå
    0
  2. LANGAZOU Messages postés 100 Statut Membre
     
    Merci pour votre réponse.

    le problème c'est que suis débutant en VBA et j'ai pas su comment intégrer ton code sur le mien :/
    0
    1. Mytå Messages postés 4246 Date d'inscription   Statut Contributeur Dernière intervention   957
       
      Re le Forum

      Essaye avec ceci :

      Private Sub UserForm_Initialize()
      Dim i As Integer, j As Integer
      Dim strTemp As String

      Me.ComboBox2.Clear

      For i = 3 To Sheets("CD").Cells(3, 2).End(xlDown).Row
      Me.ComboBox2 = Sheets("CD").Cells(i, 2)
      If Me.ComboBox2.ListIndex = -1 Then
      Me.ComboBox2.AddItem Sheets("CD").Cells(i, 2)
      End If
      Next i

      'Tri le contenu du ComboBox par ordre alphabétique
      With Me.ComboBox2
      For i = 0 To .ListCount - 1
      For j = 0 To .ListCount - 1
      If .List(i) < .List(j) Then
      strTemp = .List(i)
      .List(i) = .List(j)
      .List(j) = strTemp
      End If
      Next j
      Next i
      End With

      End Sub

      Mytå
      0
    2. greg
       
      Bonjour forumeurs,

      Petite question bête mais.. cette macro permet elle de trier par ordre alphabétique ou par ordre croissant ???
      0