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

Macros et VBA Excel Discussion :

Boucle pour remplir combobox


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Manche (Basse Normandie)

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

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Par défaut Boucle pour remplir combobox
    Bonjour,
    j'ai un petit soucis avec mes macro, il semblerait que j'ai dépassé la taille maximal.
    Je dois alimenter des combobox sans doublons, voici ma macro :
    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
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    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("A17:A" & .Range("A" & .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("B17:B" & .Range("B" & .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("C17:C" & .Range("C" & .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("D17:D" & .Range("D" & .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("E17:E" & .Range("E" & .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
     
           '6 è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.ComboBox6.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '7 è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.ComboBox7.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '8 è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.ComboBox8.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
     
           '9 è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.ComboBox9.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '10 è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.ComboBox10.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '11 ème combo'
           For Each c In .Range("K17:K" & .Range("K" & .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("L17:L" & .Range("L" & .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("M17:M" & .Range("M" & .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("N17:N" & .Range("N" & .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
     
           '15 ème combo'
           For Each c In .Range("O17:O" & .Range("O" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox15.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '16 ème combo'
           For Each c In .Range("P17:P" & .Range("P" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox16.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '17 ème combo'
           For Each c In .Range("Q17:Q" & .Range("Q" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox17.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
           '18 ème combo'
           For Each c In .Range("R17:R" & .Range("R" & .Rows.Count).End(xlUp).Row)
                tablo.Add c.Value, CStr(c.Value)
            Next c
            For Each Item In tablo
                Me.ComboBox18.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '19 è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.ComboBox19.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '20 è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.ComboBox20.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '21 è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.ComboBox21.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '22 è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.ComboBox22.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '23 è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.ComboBox23.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '24 è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.ComboBox24.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
             '25 è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.ComboBox25.AddItem Item
            Next
               'vide la collection à nouveau'
           Set tablo = Nothing
     
            On Error GoTo 0
        End With
     
     
     
     
    End Sub
    J'aimerais bien la raccourcir car j'ai encore deux trois chose a rajouter...
    Par contre les colonne ne se suivent pas, il y a un trou entre la 19ème et 20ème combo...
    Quelqu'un aurait-il une solution ?
    Merci

  2. #2
    Inactif  

    Homme Profil pro
    cuisiniste
    Inscrit en
    Avril 2009
    Messages
    15 374
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cuisiniste
    Secteur : Bâtiment

    Informations forums :
    Inscription : Avril 2009
    Messages : 15 374
    Billets dans le blog
    8
    Par défaut heu
    Bonsoir
    peut etre un truc de ce genre
    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
    Option Base 1
    Private Sub UserForm_Initialize()
        Dim tablo, tabloOUT, plage As Range, i As Long, e As Long, a As Long
        With Sheets("Feuil1")
            derlig = Range(.UsedRange.Address).SpecialCells(xlLastCell).Row
            Set plage = .Range("A17:AM" & derlig)
            tablo = plage.Value
            ReDim tabloOUT(UBound(tablo) * plage.Columns.Count, 1)
            For i = 1 To UBound(tablo)
                For e = 1 To plage.Columns.Count
                    a = a + 1
                    tabloOUT(a, 1) = tablo(i, e)
                Next e
            Next i
        End With
        ComboBox1.List = tabloOUT
    End Sub
    mes fichiers dans les contributions:
    mail avec CDO en vba et mail avec CDO en vbs dans un HTA
    survol des bouton dans userform
    prendre un cliché d'un range

    si ton problème est résolu n'oublie pas de pointer : : ça peut servir aux autres
    et n'oublie pas de voter

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Manche (Basse Normandie)

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

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Par défaut
    Bonjour,
    J'ai utilisé votre code, malheureusement il semblerait qu'il ne fonctionne pas, les combobox sont vides...

  4. #4
    Inactif  

    Homme Profil pro
    cuisiniste
    Inscrit en
    Avril 2009
    Messages
    15 374
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cuisiniste
    Secteur : Bâtiment

    Informations forums :
    Inscription : Avril 2009
    Messages : 15 374
    Billets dans le blog
    8
    Par défaut re
    re
    autant pour moi je n'avais pas vu que tu changeais de comboboxs Oupssss....

    essaie plutôt ca

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     With Sheets("Feuil1")
     For E = 1 To 19
     lastcell = Cells(Rows.Count, E).End(xlUp)
     tablo = Range(Cells(17, E), lastcel).Value
     Me.Controls("combobox" & E).List = tablo
      Next
     
     
      For E = 34 To 39
     lastcell = Cells(Rows.Count, E).End(xlUp)
     tablo = Range(Cells(17, E), lastcel).Value
     Me.Controls("combobox" & E - 14).List = tablo
      Next
    mes fichiers dans les contributions:
    mail avec CDO en vba et mail avec CDO en vbs dans un HTA
    survol des bouton dans userform
    prendre un cliché d'un range

    si ton problème est résolu n'oublie pas de pointer : : ça peut servir aux autres
    et n'oublie pas de voter

  5. #5
    Membre averti
    Homme Profil pro
    Ingénieur qualité méthodes
    Inscrit en
    Mars 2015
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Manche (Basse Normandie)

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

    Informations forums :
    Inscription : Mars 2015
    Messages : 58
    Par défaut
    Merci ^^

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2010] Boucle pour remplir un tableau
    Par bmsar dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 05/08/2013, 17h27
  2. [Débutant] Code c# : pour remplir (combobox) à partir d'une BDD postgresql
    Par geoinformation dans le forum C#
    Réponses: 11
    Dernier message: 22/12/2012, 13h53
  3. boucle pour remplir combobox par oui et non
    Par panda78 dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 06/08/2012, 15h29
  4. Comment réaliser une boucle pour remplir un tableau
    Par LVChatel dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 03/04/2009, 12h20
  5. Pb pour remplir combobox
    Par spoyet dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 21/06/2007, 17h40

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