Optimiser une répétition
bonjour,
Demande un peu particulière, dans un document, je me suis amusé à copier/coller en grosse quantité pour arriver à un code comme suit :
If UserFormLigne1.ComboBox1 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne1.ComboBox1 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne1.ComboBox2 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne1.ComboBox2 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne1.ComboBox3 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne1.ComboBox3 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne1.ComboBox4 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne1.ComboBox4 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
'Modification ligne 2
If UserFormLigne2.ComboBox1 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne2.ComboBox1 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne2.ComboBox2 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne2.ComboBox2 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne2.ComboBox3 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne2.ComboBox3 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne2.ComboBox4 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne2.ComboBox4 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
'Modification ligne 3
If UserFormLigne3.ComboBox1 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne3.ComboBox1 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne3.ComboBox2 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne3.ComboBox2 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne3.ComboBox3 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne3.ComboBox3 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
If UserFormLigne3.ComboBox4 = "Protection auditive" Then
If protectaudio1 = True Then
TBprotectaudio1.Value = protectaudio1.Caption
Else
TBprotectaudio1.Value = ""
End If
If protectaudio2 = True Then
TBprotectaudio2.Value = "," & protectaudio2.Caption
Else
TBprotectaudio2.Value = ""
End If
If protectaudio3 = True Then
TBprotectaudio3.Value = "," & protectaudio3.Caption
Else
TBprotectaudio3.Value = ""
End If
If protectaudio4 = True Then
TBprotectaudio4.Value = "," & protectaudio4.Caption
Else
TBprotectaudio4.Value = ""
End If
UserFormLigne3.ComboBox4 = "Protection auditive (" & TBprotectaudio1.Value & TBprotectaudio2.Value & TBprotectaudio3.Value & TBprotectaudio4.Value & ")"
End If
Est-ce que c'est possible de créer une boucle ou autre (vous vous y connaissez mieux que moi lol) pour raccourcir le code.
Je confirme, si vous avez une solution ce serai génial !! J'ai un exemple comme celui indiqué au dessus qui me fait arrivé à 3090 lignes ^^' donc VBA me dit procédure trop longue T.T Je compte sur vous !! =D
Merci !
Bonjour azranoth,
Difficile de t'aider efficacement sans en connaitre plus sur le projet.
A tout hasard, je te propose quelque chose comme :
Sub Boucle_GVS()
Dim i As Integer, j As Integer
Dim TBprotectaudio(4) As String
For i = 1 To 4
If UserFormLigne1.Controls("ComboBox" & i) = "Protection auditive" Then
For j = 1 To 4
If UserFormLigne1.Controls("protectaudio" & j) = True Then
TBprotectaudio(j - 1) = UserFormLigne1.Controls("protectaudio" & j).Caption
Else
TBprotectaudio(j - 1) = ""
End If
Next
UserFormLigne1.Controls("ComboBox" & i) = "Protection auditive ("
For j = 0 To 3
UserFormLigne1.Controls("ComboBox" & i) = UserFormLigne1.Controls("ComboBox" & i) & TBprotectaudio(j)
Next
UserFormLigne1.Controls("ComboBox" & i) = UserFormLigne1.Controls("ComboBox" & i) & ")"
End If
Next
End Sub
Bonjour,
Désolé du retard...
Oui je suis désolé je peux pas fournir le document ni de capture sans être sûr de pas divulguer d'information confidentielle ...
If UserFormLigne1.ComboBox1 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
UserFormLigne1.ComboBox1 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
If UserFormLigne1.ComboBox2 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
UserFormLigne1.ComboBox2 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
If UserFormLigne1.ComboBox3 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
UserFormLigne1.ComboBox3 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
If UserFormLigne1.ComboBox4 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
UserFormLigne1.ComboBox4 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
Pour essayer de résumer :
J'ai ce code (que j'ai fais évolué depuis mon 1er message). A ce stade, il faudrait que je puisse faire la même opération pour userformligne1 à userformligne15. Et si possible la même solution pour combobox 1 à 4.
Quand je sélectionne "Lunette de protection" dans l'un des 4 combobox d'un userformligneX (1 à 15) , j'ai un second userform qui s'ouvre avec les références de lunettes. Chaque référence est un checkbox appelé "lunette" (dont j'ai remplacé le caption par la référence) et un textbox appelé "TBlunette" pour venir dans le combobox choisi (celui dans lequel j'ai sélectionné "Lunette de protection").
J'ai donc construit le code ainsi :
- Vérifier dans quel combobox j'ai choisi "Lunette de protection"
- Vérifier quel checkbox j'ai choisi (7 possibilités de références possible d'où epi = 1 à 7)
- En cliquant sur un commandbutton, venir placer dans les TBlunettes concernées le caption des checkbox sélectionné
- Venir mettre dans le combobox le texte combiné de chaque TBlunettes
Aujourd'hui, je copie ce code 15 fois en remplaçant juste UserformLigne1 par UserformLigne2 etc
Encore toutes mes excuses pour le document...
Bonsoir Azranoth,
Pour dire vrai, je n'ai pas encore tout compris...
J'ai testé le code suivant sur une maquette avec ce que j'ai compris ;
Code à coller dans un module :
Sub Boucle2_GVS(oUSF As Object)
Const cNbEpis = 7
Const cNbCombos = 4
Dim iCombo As Integer, epi As Integer
Dim TBLunette(cNbEpis) As String
'On efface le contenu du tableau local
Erase TBLunette
'Boucle sur les combobox
For iCombo = 1 To cNbCombos
If oUSF.Controls("ComboBox" & iCombo) = "Lunette de protection" Then
'Boucle sur les EPI
For epi = 1 To cNbEpis
If oUSF.Controls("lunette" & epi) = True Then
'On stocke le libellé de la case à cocher
TBLunette(epi) = oUSF.Controls("lunette" & epi).Caption
End If
Next
End If
'On renseigne le texte du combobox traité
oUSF.Controls("ComboBox" & iCombo) = "Lunette de protection ("
For epi = 0 To cNbEpis - 1
oUSF.Controls("ComboBox" & iCombo) = oUSF.Controls("ComboBox" & iCombo) & TBLunette(epi)
Next
oUSF.Controls("ComboBox" & iCombo) = oUSF.Controls("ComboBox" & iCombo) & ")"
Next
End Sub
et pour activer cette boucle, un bouton dans une des userforms avec le code :
Private Sub CommandButton1_Click()
Dim o As Object
For Each o In UserForms
Boucle2_GVS o
Next
End Sub
Bonjour,
Oui toutes mes excuses les plus plates de pas pouvoir donner le fichier ou même des captures...
Et un très grand merci d'essayer malgré tout !!
alors j'ai testé ton code :
If oUSF.Controls("lunette" & epi) = True Then
Il me surligne ce passage avec un message d'erreur "Objet spécifié introuvable".
Tu utilises beaucoup d'éléments "trop avancés" pour mon niveau alors je suis pas sûr de tout comprendre. Mais je pense que tu as compris ce que je voulais faire.
Après, ce qu'il faut vraiment c'est pouvoir boucler ce code (celui que j'utilise) sur les ligne 1 à 15.
J'avais essayé quelques trucs dans le genre :
For usf = 1 To 15
With "UserFormLigne" & usf
If ComboBox1 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
ComboBox1 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
End With
Next
Mais dans ce cas mon combobox1 reste simplement avec "Lunette de protection" (ce que j'ai choisi dans la liste). Il ne complète pas avec les TBlunettes.
Le code suivant fonctionne bien : (même exemple mais j'ai pris la combobox3 là)
If UserFormLigne1.ComboBox3 = "Lunette de protection" Then
For epi = 1 To 7
If Me.Controls("lunette" & epi) = True Then
Me.Controls("TBlunette" & epi).Value = Me.Controls("lunette" & epi).Caption
End If
Next
UserFormLigne1.ComboBox3 = "Lunette de protection (" & TBlunette1.Value & " " & TBlunette2.Value & " " & TBlunette3.Value & " " & TBlunette4.Value & " " & TBlunette5.Value & " " & TBlunette6.Value & " " & TBlunette7.Value & ")"
End If
Mais ce qui est gênant c'est que je dois copié/collé ce code 15 fois en remplaçant UserformLigne1 par UserformLigne2 / 3 etc jusque 15.
Bonjour Azranoth,
Ultime tentative pour te venir en aide:
Concernant l’erreur « Objet spécifié introuvable », vérifies soigneusement l’appellation de tes contrôles dans les userforms: en l’occurrence « lunette1 » ... »lu « lunette7 » (le nom des contrôles est sensible à la casse.
Bonjour,
Oui oui, le nom des contrôles est bien identique.
C'est pas grave merci beaucoup d'avoir essayé de m'aider !!