IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Excel Discussion :

Trier des combobox par ordre croissant et alphabétique


Sujet :

Excel

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur qualité méthodes

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Points : 25
    Points
    25
    Par défaut Trier des combobox par ordre croissant et alphabétique
    Bonjour,
    J'utilise des user form avec plusieurs comboboxs qui s'alimentent automatiquement à l'aide de mon tableur excel.
    Pour cela j'utilise le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Private Sub UserForm_Initialize()
    Dim c As Range
    Dim tablo As New Collection
    On Error Resume Next
    With Sheets("Feuil1")
    For Each c In .Range("C17:C" & .Range("C" & .Rows.Count).End(xlUp).Row)
        tablo.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0
    For Each Item In tablo
        Me.ComboBox1.AddItem Item
    Next
    End With
    End Sub
    Je voudrais savoir que faut-il que j'ajoute à ce type de code pour pouvoir trier par ordre croissant sur certain combobox et sur d'autre pouvoir faire un tri par ordre alphabétique ?
    Merci d'avance

  2. #2
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 617
    Points : 5 912
    Points
    5 912
    Par défaut
    Bonjour,

    Après avoir rempli tes combobox, tu peux appeler cette macro de tri
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    Sub Trier(Cbo As ComboBox, Alpha As Boolean)
        Dim I As Long, J As Long
        Dim Tmp
     
        If Alpha = True Then
            For I = 0 To Cbo.ListCount - 1
                For J = I To Cbo.ListCount - 1
                    If Cbo.List(J) < Cbo.List(I) Then
                        Tmp = Cbo.List(I)
                        Cbo.List(I) = Cbo.List(J)
                        Cbo.List(J) = Tmp
                    End If
                Next
            Next
        Else
            For I = 0 To Cbo.ListCount - 1
                For J = I To Cbo.ListCount - 1
                    If CDbl(Cbo.List(J)) < CDbl(Cbo.List(I)) Then
                        Tmp = Cbo.List(I)
                        Cbo.List(I) = Cbo.List(J)
                        Cbo.List(J) = Tmp
                    End If
                Next
            Next
        End If
    End Sub
    Pour l'appeler, tu mets ceci en changeant le nom du combobox à trier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Trier ComboBox1, False ' si numérique
    Trier ComboBox1, True  ' si alphabétique
    MPi²

  3. #3
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Septembre 2013
    Messages
    1 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2013
    Messages : 1 369
    Points : 2 156
    Points
    2 156
    Par défaut
    Bonjour,

    La méthode présentée ci dessus est lente pour 2 raisons (3,5 sec pour 500 noms):

    -Algorithme de tri le plus lent qui existe.
    -Il ne faut pas trier sur le combobox lui même mais dans un tableau

    Avec la méthode ci dessous, 0,1 s pour 5000 noms


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    Private Sub UserForm_Initialize()
      Set f = Sheets("BD")
      a = Application.Transpose(f.Range("A2:A" & f.[A65000].End(xlUp).Row))
      Call Tri(a, LBound(a), UBound(a))
      Me.ComboBox1.List = a
    End Sub
     
    Sub Tri(a, gauc, droi)          ' Quick sort
     ref = a((gauc + droi) \ 2)
     g = gauc: d = droi
     Do
         Do While a(g) < ref: g = g + 1: Loop
         Do While ref < a(d): d = d - 1: Loop
         If g <= d Then
           temp = a(g): a(g) = a(d): a(d) = temp
           g = g + 1: d = d - 1
         End If
     Loop While g <= d
     If g < droi Then Call Tri(a, g, droi)
     If gauc < d Then Call Tri(a, gauc, d)
    End Sub
    S'il y a des doublons:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    Private Sub UserForm_Initialize()
      Set f = Sheets("BD")
      Set mondico = CreateObject("Scripting.Dictionary")
      a = f.Range("A2:A" & f.[A65000].End(xlUp).Row)  ' tableau a(n,1) pour rapidité
      For i = LBound(a) To UBound(a)
       If a(i, 1) <> "" Then mondico(a(i, 1)) = ""
      Next i
     '--avec tri
      temp = mondico.keys
      Call Tri(temp, LBound(temp), UBound(temp))
      Me.ComboBox1.List = temp
    End Sub
    Jacques BOISGONTIER
    Fichiers attachés Fichiers attachés

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur qualité méthodes

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Points : 25
    Points
    25
    Par défaut
    Bonjour j'ai donc insérer votre code dans mon userform et rajouter ceci à la fin :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
       '14 ème combo'
           For Each c In .Range("AM17:AM" & .Range("AM" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox14.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
                On Error GoTo 0
                                  
        End With
        Trier ComboBox5, False ' si numérique
    End Sub
    Le problème est que quand je met ceci mon userform ne s'ouvre pas...

  5. #5
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 617
    Points : 5 912
    Points
    5 912
    Par défaut
    Je viens de faire un test avec le code suivant avec un Userform qui ne contient qu'un combobox5 et ça fonctionne bien.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Private Sub UserForm_Initialize()
        Dim C As Range
        Dim tablo As New Collection
        Dim Item As Variant
     
        With ActiveSheet
            On Error Resume Next
            For Each C In .Range("AM17:AM" & .Range("AM" & .Rows.Count).End(xlUp).Row)
                 tablo.Add C.Value, CStr(C.Value)
            Next C
            For Each Item In tablo
                Me.ComboBox5.AddItem Item
            Next
               'vide la collection à nouveau'
            Set tablo = Nothing
     
            On Error GoTo 0
     
        End With
        Trier ComboBox5, False ' si numérique
    End Sub
    MPi²

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur qualité méthodes

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Points : 25
    Points
    25
    Par défaut
    Voici mon code entier avec à la fin le tri :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    Private Sub UserForm_Initialize()
     
        Dim c As Range
        Dim tablo As New Collection
        Dim Item
        On Error Resume Next
        With Sheets("Feuil1")
             '1er combo
           For Each c In .Range("C17:C" & .Range("C" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox1.AddItem Item
            Next
            'vide la collection pour une seconde utilisation (un dictionnaire est plus rapide !)
           Set tablo = Nothing
     
            '2 ème combo
           For Each c In .Range("E17:E" & .Range("E" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox2.AddItem Item
            Next
            'vide la collection à nouveau
           Set tablo = Nothing
     
            '3 ème combo
           For Each c In .Range("F17:F" & .Range("F" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox3.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '4 ème combo'
           For Each c In .Range("G17:G" & .Range("G" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox4.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '5 ème combo'
           For Each c In .Range("H17:H" & .Range("H" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox5.AddItem Item
     
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
              Trier ComboBox5, False ' si numérique
           '6 ème combo'
           For Each c In .Range("I17:I" & .Range("I" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox6.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '7 ème combo'
           For Each c In .Range("J17:J" & .Range("J" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox7.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '8 ème combo'
           For Each c In .Range("AH17:AH" & .Range("AH" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox8.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '9 ème combo'
           For Each c In .Range("S17:S" & .Range("S" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox9.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '10 ème combo'
          For Each c In .Range("AI17:AI" & .Range("AI" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox10.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '11 ème combo'
           For Each c In .Range("AJ17:AJ" & .Range("AJ" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox11.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '12 ème combo'
           For Each c In .Range("AK17:AK" & .Range("AK" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox12.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '13 ème combo'
           For Each c In .Range("AL17:AL" & .Range("AL" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox13.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '14 ème combo'
           For Each c In .Range("AM17:AM" & .Range("AM" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox14.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
                On Error GoTo 0
     
        End With
    Trier ComboBox5, False ' si numérique
    End Sub
    Du coup quand je clique pour appeler mon userform sur mon bouton il ne s'ouvre pas mon tableur "clignote"

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur qualité méthodes

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Points : 25
    Points
    25
    Par défaut
    C'est à ne rien y comprendre j'ai enlevé On Error GoTo 0, sa a marché, je l'ai remis et sa marche encore alors que tout à l'heure non.

Discussions similaires

  1. Trier des lignes par ordre alphabétique ?
    Par Evocatii dans le forum Eclipse
    Réponses: 0
    Dernier message: 11/08/2007, 11h26
  2. trier une ComboBox par ordre alphabétique
    Par poussin_44 dans le forum Windows Forms
    Réponses: 4
    Dernier message: 11/07/2007, 17h23
  3. Trier un combobox par ordre alphabétique
    Par alex.a dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 20/06/2007, 10h04
  4. organiser un combobox par ordre croissant??
    Par shadow31 dans le forum MFC
    Réponses: 2
    Dernier message: 18/05/2005, 09h31
  5. Trier un tableau par ordre croissant
    Par Halleck dans le forum Algorithmes et structures de données
    Réponses: 15
    Dernier message: 01/11/2004, 00h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo