Incrémenter numéro combobox

Résolu
lullierjon Messages postés 3 Statut Membre -  
cs_Le Pivert Messages postés 8437 Statut Contributeur -


Bonjour,

J'ai la même question. Je voudrais alléger mon code car j'ai 97 combobox à faire...
Chaque ComboBox représente chaque colonne : ComboBox 1 = Colonne A de Sheets("Liste")

Voici mon code de mon UserForm3 :

Dim colonne As Integer, i As Integer, j As Integer

dernlig = Sheets("Liste").Range("A" & Rows.Count).End(xlUp).Row
derncol = Sheets("Liste").Cells(1, Cells.Columns.Count).End(xlToLeft).Column
'MsgBox "dernlig = " & dernlig & Chr(13) & "derncol = " & derncol
i = 2
j = i
Do While Sheets("Liste").Cells(j, 1).Value <> ""
UserForm3.ComboBox1.AddItem Sheets("Liste").Cells(j, 1)
j = j + 1
Loop
j = i
Do While Sheets("Liste").Cells(j, 2).Value <> ""
UserForm3.ComboBox2.AddItem Sheets("Liste").Cells(j, 2)
j = j + 1
Loop
j = i
Do While Sheets("Liste").Cells(j, 3).Value <> ""
UserForm3.ComboBox3.AddItem Sheets("Liste").Cells(j, 3)
j = j + 1
Loop
j = i
Do While Sheets("Liste").Cells(j, 4).Value <> ""
UserForm3.ComboBox4.AddItem Sheets("Liste").Cells(j, 4)
j = j + 1
Loop
j = i
Do While Sheets("Liste").Cells(j, 5).Value <> ""
UserForm3.ComboBox5.AddItem Sheets("Liste").Cells(j, 5)
j = j + 1
Loop
End Sub

Private Sub ToggleButton1_Click()
Unload Me
End Sub


Si vous trouver ma solution, ça va pouvoir s'écrire à peu près comme ça :

For i = 1 to derncol
j = 2 'Bloquer j = 2 au démarrage de la boucle
Do While Sheets("Liste").Cells(j, i).Value <> ""
UserForm3.ComboBox & i.AddItem Sheets("Liste").Cells(j, i) 'C'est là que ça coince ! Je voudrais savoir écrire le bon script du ComboBox
j = j + 1
Loop
next i


merci par avance de votre coup de main
Configuration: Windows / Firefox 75.0

3 réponses

  1. lullierjon Messages postés 3 Statut Membre 1
     
    J'ai lu mais je m'y perd dans tous ces cas de figures...
    1
    1. cs_Le Pivert Messages postés 8437 Statut Contributeur 730
       
      un exemple pour remplir 6 combo avec 6 lignes sur 6 colonnes

       Dim i As Integer
        Dim j As Integer
           For i = 1 To 6 'colonnes
           For j = 1 To 6 'lignes
        Me.Controls("ComboBox" & i).AddItem Sheets("Feuil1").Cells(j, i)
          Me.Controls("ComboBox" & i).ListIndex = 0
          Next j
         Next i


      a adapter

      @+ Le Pivert
      0
  2. lullierjon Messages postés 3 Statut Membre 1
     
    Je n'avais pas vu que il y avait eu une modification de votre réponse...
    J'ai fait ça !

    Option Explicit
    Option Explicit
    Dim dernlig As Integer, derncol As Integer, i As Integer, j As Integer
    Private Sub UserForm_Initialize()
    derncol = Sheets("Liste").Cells(1, Cells.Columns.Count).End(xlToLeft).Column
        For i = 1 To derncol 'Numéro des colonnes
    dernlig = Sheets("Liste").Cells(Rows.Count, i).End(xlUp).Row
            For j = 2 To dernlig 'Numéro des lignes
                    Me.Controls("ComboBox" & i).AddItem Sheets("Liste").Cells(j, i)
                    'Me.Controls("ComboBox" & i).ListIndex = 0
            Next j
        Next i
    End Sub
    


    C'est exactement votre travail.
    Cependant je voudrais savoir à quoi sert cette ligne :

    Me.Controls("ComboBox" & i).ListIndex = 0
    0