Comment faire une boucle dans une userform

Sahmy021 Messages postés 17 Statut Membre -  
Patrice33740 Messages postés 8400 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour
Je désire faire une boucle dans une userform.
J'ai deux combobox qui s'enchaine et la liste de la 2nd dépends du choix dans la première.
J'ai besoin de faire 15 ligne identique dans mon formulaire. j'aimerais éviter de mettre 15 fois le meme code en changeant juste les num de mes combobox

ci-dessous mon code pour les deux premières :

Private Sub CbbxLigneBudg1_Change()
    If CbbxLigneBudg1.Value <> "" Then
        CbbxSsLigneBudg1.Enabled = True
    Else
        CbbxSsLigneBudg1.Enabled = False
    End If
    
    If CbbxLigneBudg1.Value = "Ass. Plongeur" Then
        CbbxSsLigneBudg1.RowSource = "LstAssPlongeur"
    ElseIf CbbxLigneBudg1.Value = "Vètement" Then
        CbbxSsLigneBudg1.RowSource = "LstVetement"
    ElseIf CbbxLigneBudg1.Value = "Reglt Plongées" Then
        CbbxSsLigneBudg1.RowSource = "LstregltPlongée"
    ElseIf CbbxLigneBudg1.Value = "Boissons" Then
        CbbxSsLigneBudg1.RowSource = "LstBoisson"
    ElseIf CbbxLigneBudg1.Value = "Licence" Then
        CbbxSsLigneBudg1.RowSource = "LstLicence1"
    ElseIf CbbxLigneBudg1.Value = "Sorties Voyages" Then
        CbbxSsLigneBudg1.RowSource = "LstSorties"
    ElseIf CbbxLigneBudg1.Value = "Formation" Then
        CbbxSsLigneBudg1.RowSource = "LstFormation"
    ElseIf CbbxLigneBudg1.Value = "Fonc. Admin" Then
        CbbxSsLigneBudg1.RowSource = "LstFoncadmin"
    ElseIf CbbxLigneBudg1.Value = "Manifestation" Then
        CbbxSsLigneBudg1.RowSource = "LstManif"
    ElseIf CbbxLigneBudg1.Value = "Fonc. Activité" Then
        CbbxSsLigneBudg1.RowSource = "LstFoncAct"
    ElseIf CbbxLigneBudg1.Value = "Charges" Then
        CbbxSsLigneBudg1.RowSource = "LstCharge"
    ElseIf CbbxLigneBudg1.Value = "Subvention Cotisation" Then
        CbbxSsLigneBudg1.RowSource = "LstSubvCotis"
    End If
End Sub

Private Sub CbbxLigneBudg2_Change()
    If CbbxLigneBudg2.Value <> "" Then
        CbbxSsLigneBudg2.Enabled = True
    Else
        CbbxSsLigneBudg2.Enabled = False
    End If
    
    If CbbxLigneBudg2.Value = "Ass. Plongeur" Then
        CbbxSsLigneBudg2.RowSource = "LstAssPlongeur"
    ElseIf CbbxLigneBudg2.Value = "Vètement" Then
        CbbxSsLigneBudg2.RowSource = "LstVetement"
    ElseIf CbbxLigneBudg2.Value = "Reglt Plongées" Then
        CbbxSsLigneBudg2.RowSource = "LstregltPlongée"
    ElseIf CbbxLigneBudg2.Value = "Boissons" Then
        CbbxSsLigneBudg2.RowSource = "LstBoisson"
    ElseIf CbbxLigneBudg2.Value = "Licence" Then
        CbbxSsLigneBudg2.RowSource = "LstLicence2"
    ElseIf CbbxLigneBudg2.Value = "Sorties Voyages" Then
        CbbxSsLigneBudg2.RowSource = "LstSorties"
    ElseIf CbbxLigneBudg2.Value = "Formation" Then
        CbbxSsLigneBudg2.RowSource = "LstFormation"
    ElseIf CbbxLigneBudg2.Value = "Fonc. Admin" Then
        CbbxSsLigneBudg2.RowSource = "LstFoncadmin"
    ElseIf CbbxLigneBudg2.Value = "Manifestation" Then
        CbbxSsLigneBudg2.RowSource = "LstManif"
    ElseIf CbbxLigneBudg2.Value = "Fonc. Activité" Then
        CbbxSsLigneBudg2.RowSource = "LstFoncAct"
    ElseIf CbbxLigneBudg2.Value = "Charges" Then
        CbbxSsLigneBudg2.RowSource = "LstCharge"
    ElseIf CbbxLigneBudg2.Value = "Subvention Cotisation" Then
        CbbxSsLigneBudg2.RowSource = "LstSubvCotis"
    End If
End Sub


Merci d'avance

EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.

1 réponse

  1. Utilisateur anonyme
     
    Bonsoir,

    faire une boucle n'est pas la solution pour ne pas répéter un code comme le tien.
    Il faut mettre le code dans une sub ou une fonction qui prend en paramètre les combos.
    Cette fonction tu l'appelles de chaque évènement Change avec les paramètres adéquats.

    Sub GestionCombo(Source as ComboBox, Cible as ComboBox
        If Source.Value <> "" Then
            Cible.Enabled = True
        Else
            Cible.Enabled = False
        End If
        
        If Source.Value = "Ass. Plongeur" Then
            Cible.RowSource = "LstAssPlongeur"
        ElseIf Source.Value = "Vètement" Then
            Cible.RowSource = "LstVetement"
        ElseIf Source.Value = "Reglt Plongées" Then
            Cible.RowSource = "LstregltPlongée"
        ElseIf Source.Value = "Boissons" Then
            Cible.RowSource = "LstBoisson"
        ElseIf Source.Value = "Licence" Then
            Cible.RowSource = "LstLicence1"
        ElseIf Source.Value = "Sorties Voyages" Then
            Cible.RowSource = "LstSorties"
        ElseIf Source.Value = "Formation" Then
            Cible.RowSource = "LstFormation"
        ElseIf Source.Value = "Fonc. Admin" Then
            Cible.RowSource = "LstFoncadmin"
        ElseIf Source.Value = "Manifestation" Then
            Cible.RowSource = "LstManif"
        ElseIf Source.Value = "Fonc. Activité" Then
            Cible.RowSource = "LstFoncAct"
        ElseIf Source.Value = "Charges" Then
            Cible.RowSource = "LstCharge"
        ElseIf Source.Value = "Subvention Cotisation" Then
            Cible.RowSource = "LstSubvCotis"
        End If
    End Sub


    Private Sub CbbxLigneBudg1_Change()
    GestionCombo CbbxLigneBudg1, CbbxSsLigneBudg1
    End Sub

    Private Sub CbbxLigneBudg2_Change()
    GestionCombo CbbxLigneBudg2.Value, CbbxSsLigneBudg2
    End Sub

    Par contre tu peux alléger ta cascade de If en utilisant un Select Case.
    1
    1. Sahmy021 Messages postés 17 Statut Membre
       
      Merci ! ca marche bien.
      Du coup je suis qd même obligé d'enchainer les 15 "private sub" ?
      J'ai vu des chose sur le select case
      je vais fouiner un peux pour voir comment cela fonctionne
      encore merci
      0
    2. Sahmy021 Messages postés 17 Statut Membre
       
      Du coup j'ai tapé ca :

      Select Case Source.Value
              Case Is = "Ass. Plongeur"
                  Cible.RowSource = "LstAssPlongeur"
              Case Is = "Vètement"
                  Cible.RowSource = "LstVetement"
              Case Is = "Reglt Plongées"
                  Cible.RowSource = "LstregltPlongée"
              Case Is = "Boissons"
                  Cible.RowSource = "LstBoisson"
              Case Is = "Licence"
                  Cible.RowSource = "LstLicence1"
              Case Is = "Sorties Voyages"
                  Cible.RowSource = "LstSorties"
              Case Is = "Formation"
                  Cible.RowSource = "LstFormation"
              Case Is = "Fonc. Admin"
                  Cible.RowSource = "LstFoncadmin"
              Case Is = "Manifestation"
                  Cible.RowSource = "LstManif"
              Case Is = "Fonc. Activité"
                  Cible.RowSource = "LstFoncAct"
              Case Is = "Charges"
                  Cible.RowSource = "LstCharge"
              Case Is = "Subvention Cotisation"
                  Cible.RowSource = "LstSubvCotis"
          End Select


      un peu plus simple mais toujours autant de ligne
      0
    3. Utilisateur anonyme
       
      Tu pourrais écrire un tableau à 2 dimension, dans la 1ere dimension tu stokes les sources possibles et dans la seconde les rowSource possibles.

      Tu fais une boucle For, pour tester les sources, quand tu trouves la bonnes, tu appliques le RowSource du même index.

      VBA, c'est loin pour moi, je ne suis pas capable de te faire un exemple de tête (contrairement à tout à l'heure) et je n'ai pas de PC sous la main.
      0
    4. Sahmy021 Messages postés 17 Statut Membre
       
      Ce sera tres bien comme ca ;-)
      Moi c'est ma première sur VBA ! et je m'étais juré de jamais y mettre les mains lol

      Pour le reste y a-t-il moyen de faire une boucle pour éviter de faire les 15 commandes de private sub?
      0