Modification d'un tableau grace à un userform
Bonsoir
j'ai construit un userform pour modifier les données d'un tableau.
chaque ligne correspond à un dossier différent.
or, j'arrive à rapatrier les données du tableau dans le userform mais lorsque je renvoies les nouvelles données dans le tableu seul les deux textbox modifient les données. les combobox ne font rien.
voici mon code:
Private Sub ComboBox29_Change()
Dim ligne As Integer
Dim i As Integer
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
TextBox1.Value = .Range("B" & ligne).Value
TextBox2.Value = .Range("C" & ligne).Value
ComboBox29.Value = .Range("D" & ligne).Value
ComboBox11.Value = .Range("E" & ligne).Value
TextBox5.Value = .Range("F" & ligne).Value
ComboBox5.Value = .Range("H" & ligne).Value
ComboBox28.Value = .Range("I" & ligne).Value
ComboBox7.Value = .Range("J" & ligne).Value
ComboBox27.Value = .Range("K" & ligne).Value
ComboBox10.Value = .Range("x" & ligne).Value
End With
End If
End Sub
Private Sub CommandButton22_Click()
Dim ligne As Integer
Dim i As Integer
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("B" & ligne).Value = TextBox1.Value
.Range("C" & ligne).Value = TextBox2.Value
.Range("D" & ligne).Value = ComboBox29.Value
.Range("E" & ligne).Value = ComboBox11.Value
.Range("F" & ligne).Value = TextBox5.Value
.Range("H" & ligne).Value = ComboBox5.Value
.Range("I" & ligne).Value = ComboBox28.Value
.Range("J" & ligne).Value = ComboBox7.Value
.Range("K" & ligne).Value = ComboBox27.Value
.Range("X" & ligne).Value = ComboBox10.Value
End With
End Ifmerci de votre aide
Bonjour et bienvenue sur le forum
Pourrrais-tu joindre le fichier complet ?
A te relire.
Bye !
Voila j'ai allégé au maximum le fichier donc plus grand chose doit fonctionner lol
le code que je travaille est dans l'userform2 et le bouton qui le commande est "modifier un dossier"
Bonjour
Ajoute ces quelques lignes de codes :
Dim flag
Private Sub ComboBox29_Change()
Dim ligne As Integer
Dim i As Integer
If flag = 1 Then
flag = 0
Exit Sub
End If
i = 1
ligne = -1le reste inchangé
Private Sub CommandButton22_Click()
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1....le reste inchangé
Essaie et dis-moi le résultat...
Bye !
Bye !
Bonjour,
Il faut que tu effaces la source de données dans la propriété RowSource du contrôle ComboBox29 (AVP!D:D).
Pour alimenter la ComboBox29, ajoute ce code dans le module Userform2 :
Private Sub UserForm_Initialize()
With Worksheets("AVP")
Me.ComboBox29.List() = .Range("D2:D" & .Range("D" & Rows.Count).End(xlUp).Row).Value
End With
End SubA+
Merci à vous deux j'ai fais ce que vous avez demandé et ça marche super
pouvez vous m'expliquer ce que j'ai oublié?
je ne comprend pas ce que fait flag
et pourquoi il fallait changer le RowSource?
que signifie de mettre Me. ?
Si tu renseignes la propriété RowSource de la ComboBox29 avec AVP!D:D, lorsque tu changes une valeur dans la colonne D la liste des valeurs de la ComboBox29 change également.
Ce changement de valeur lance la procédure ComboBox29_Change().
Or, lorsque tu valides tes modifications, tu viens copier la valeur de la ComboBox29 dans la colonne D avec la ligne de code
.Range("D" & ligne).Value = ComboBox29.ValueLa colonne D étant modifiée, la procédure ComboBox29_Change() est lancée et les valeurs des contrôles sont remplacées par les valeurs de la feuille.
En clair, ça se mord la queue.
Me. avant le nom du contrôle, indique que l’on fait référence à l’Userform.
Me.ComboBox29.Value est équivalent à Userform2.ComboBox29.Value.
Dans le cas présent, tu peux ne pas le spécifier car il n'y a pas d’ambiguïté mais avec Me c’est vachement mieux.
A+
ok donc il vaut mieux que je rajoute Me. devant tout ou je laisse comme ça?
Tu peux laisser comme ça puisqu'il n'y a pas de confusion possible.
A+
Vous allez dire que je suis chiante mais du coup j'ai décidé d'utiliser ce code pour remplir la suite des colonnes (il y a 65 colonnes dans ce tableau). Comme les inforlmations arrivent en plusierus temps je me suis dit que ce code été pratique pour moi.
voici ce que j'ai fais pour un nouvel userform que j'ai créé:
Dim flag
Private Sub ComboBox29_Change()
Dim ligne As Integer
Dim i As Integer
If flag = 1 Then
flag = 0
Exit Sub
End If
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
TextBox1.Value = .Range("B" & ligne).Value
TextBox2.Value = .Range("C" & ligne).Value
ComboBox29.Value = .Range("D" & ligne).Value
End With
End If
End Sub
Private Sub CommandButton22_Click()
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
If CheckBox1.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("N" & ligne).Value = TextBox3.Value
.Range("P" & ligne).Value = ComboBox30.Value
.Range("Z" & ligne).Value = TextBox5.Value
End If
If CheckBox2.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("V" & ligne).Value = ComboBox27.Value
.Range("T" & ligne).Value = TextBox13.Value
.Range("AA" & ligne).Value = TextBox51.Value
.Range("AB" & ligne).Value = TextBox52.Value
.Range("AC" & ligne).Value = TextBox53.Value
End If
If CheckBox3.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("V" & ligne).Value = ComboBox27.Value
.Range("T" & ligne).Value = TextBox13.Value
.Range("AD" & ligne).Value = TextBox54.Value
.Range("AE" & ligne).Value = TextBox55.Value
.Range("AF" & ligne).Value = TextBox56.Value
.Range("AG" & ligne).Value = TextBox57.Value
.Range("AH" & ligne).Value = TextBox58.Value
End If
If CheckBox4.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("V" & ligne).Value = ComboBox27.Value
.Range("T" & ligne).Value = TextBox13.Value
.Range("AI" & ligne).Value = TextBox59.Value
.Range("AJ" & ligne).Value = TextBox60.Value
.Range("AK" & ligne).Value = TextBox61.Value
.Range("AL" & ligne).Value = TextBox62.Value
.Range("AM" & ligne).Value = TextBox63.Value
.Range("AN" & ligne).Value = TextBox64.Value
End If
If CheckBox5.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("V" & ligne).Value = ComboBox27.Value
.Range("T" & ligne).Value = TextBox13.Value
.Range("AO" & ligne).Value = TextBox65.Value
.Range("AP" & ligne).Value = TextBox66.Value
.Range("AQ" & ligne).Value = TextBox67.Value
.Range("AR" & ligne).Value = TextBox68.Value
End If
If CheckBox6.Value Is True Then
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")
.Range("V" & ligne).Value = ComboBox27.Value
.Range("T" & ligne).Value = TextBox13.Value
.Range("AS" & ligne).Value = TextBox69.Value
.Range("AT" & ligne).Value = TextBox70.Value
End If
End With
End If
'Windows("AVP_Année en cours.xlsm").Activate
'ActiveWorkbook.Save
'Sheets("Création de dossier").Select
UserForm2.Hide
Unload UserForm2
End Sub
Private Sub CommandButton4_Click()
UserForm2.Hide
End Sub
Private Sub CommandButton5_Click()
Unload UserForm2
End Sub
Private Sub UserForm_Initialize()
With Worksheets("AVP")
Me.ComboBox29.List() = .Range("D2:D" & .Range("D" & Rows.Count).End(xlUp).Row).Value
End With
End Sublorsque je lance le formulaire s'ouvre bien j'entre les info mais ca me dit ensuite: erreur de compilation: incompatibilité de type et ça surligne comme je l'ai indiqué dans le code. une idée de la raison? car j'utilise cette façon de faire dans un autre userform sur ce fichier et cela fonctionne...
D'après toi, est-il utile de répéter cette partie de code ?
Dim ligne As Integer
Dim i As Integer
flag = 1
i = 1
ligne = -1
With Sheets("AVP")
Do While (.Range("D" & i) <> ComboBox29.Value And .Range("D" & i) <> Empty)
i = i + 1
Loop
If (.Range("D" & i).Value = ComboBox29.Value) Then
ligne = i
End If
End With
If (ligne > 0) Then
With Sheets("AVP")De plus, je pense que tu as compris que tu devais joindre un fichier pour nous permettre de t'aider efficacement.
A+
à la bas j'avais pas répété cette partie mais comme ca fonctionnait pas je me suis dit qu'il fallait peut être la remettre dans chaque condition.
Comme mon fichier est trop gros pour être en entier ici et qu'il était la suite de ce que j'ai déjà mis j'ai pas pensé qu'il était nécessaire.
je vois pour tout supprimer de nouveau et mettre la partie qui intéresse ici
Je n'ai pas pu utiliser ton classeur qui renvoyait des erreurs.
J'en ai créé un autre qui te montrera comment structurer ton code.
A+
je ne comprends pas qu'as tu fais pour qu'il fonctionne?
dans ton fichier ça fonctionne et quand je copie et colle le code dans mon fichier il bloque...
Dans ton fichier, j'ai corrigé une erreur sur la TextBox correspondant à "Amphétamine".
Dans le code, elle est désignée TextBox54 alors que dans l'userform elle était nommée TextBox534.
Cela dit, si tu souhaites de l'aide il faut étayer un peu ta demande.
A minima, tu peux indiquer la ligne de blocage, le code erreur, etc.
A+
ben justement elle bloque à la textbox54 lol
j'ai pas pensé à aller voir là.
le pire c'est que j'ai vérifier 2 fois mes noms. je dois être fatiguée lol
merci de ton aide