Modification d'un enregistrement
Bonjour tout le monde;
Au boulot, j'ai été amenée à développer une petite application qui me permet d'enregistrer toutes les affaires que j'ai eu à traiter (ci-joint une image de mon formulaire).
Une fois je clique sur le bouton enregistrer, toutes les données sont enregistrées sur une feuille Excel nommée DATA.
J'ai deux questions, et comme je suis on ne peut plus novice en ce qui concerne VBA, je vous demande de l'aide:
1- Dans mon code, j'ai fait en sorte qu'il n y ait pas de doublons, ainsi, si je saisis un numéro d'affaire existant sur la feuille DATA, un message d'erreur est affiché pour me dire que c'est une affaire déjà enregistrée. Cependant, il m'arrive de vouloir modifier les données relatives à certaines affaires (mais impossible d'enregistrer puisqu'il s'agit d'un numéro d'affaire existant). Alors ma question est: Comment faire pour modifier des données préexistantes.
2- Dans les 5 premiers champs où je sélectionne le type de produit ..., je ne suis sensée pouvoir cocher qu'un seule case, alors que là, je peux sélectionner plusieurs types de produits pour une seule affaire. Comment pourrai-je faire en sorte que seule une des cases soit cochées.
Je vous remercie de votre aide!
'Bouton Enregistrer
Private Sub CommandButton1_Click()
TextBox2.Text = Format(Date, "mm/dd/yyyy")
Dim i As Integer
i = 2
Dim trouver1 As Boolean
trouver1 = False
Dim k As Integer
k = 0
While Worksheets("DATA").Cells(i, 1) <> "" And trouver1 = False
If Worksheets("DATA").Cells(i, 1) = TextBox1.Text Then
trouver1 = True
MsgBox "Affaire déjà enregistrée"
k = i
Else
i = i + 1
End If
Wend
If trouver1 = False Then
Worksheets("DATA").Cells(i, 1) = TextBox1.Text
Worksheets("DATA").Cells(i, 2) = TextBox2.Text
Worksheets("DATA").Cells(i, 32) = TextBox5.Text
Worksheets("DATA").Cells(i, 33) = TextBox3.Text
Worksheets("DATA").Cells(i, 34) = TextBox4.Text
If Worksheets("FORMULAIRE").CheckBox36.Value = True Then
Worksheets("DATA").Cells(i, 3) = "PP"
End If
If Worksheets("FORMULAIRE").CheckBox37.Value = True Then
Worksheets("DATA").Cells(i, 3) = "CA"
End If
If Worksheets("FORMULAIRE").CheckBox38.Value = True Then
Worksheets("DATA").Cells(i, 3) = "LOA"
End If
If Worksheets("FORMULAIRE").CheckBox48.Value = True Then
Worksheets("DATA").Cells(i, 4) = "PP"
End If
If Worksheets("FORMULAIRE").CheckBox49.Value = True Then
Worksheets("DATA").Cells(i, 4) = "PM"
End If
If Worksheets("FORMULAIRE").CheckBox39.Value = True Then
Worksheets("DATA").Cells(i, 5) = "PPR"
End If
If Worksheets("FORMULAIRE").CheckBox40.Value = True Then
Worksheets("DATA").Cells(i, 5) = "CMR"
End If
If Worksheets("FORMULAIRE").CheckBox41.Value = True Then
Worksheets("DATA").Cells(i, 5) = "GE"
End If
If Worksheets("FORMULAIRE").CheckBox42.Value = True Then
Worksheets("DATA").Cells(i, 5) = "BQUE"
End If
If Worksheets("FORMULAIRE").CheckBox43.Value = True Then
Worksheets("DATA").Cells(i, 6) = "Réseau1"
End If
If Worksheets("FORMULAIRE").CheckBox44.Value = True Then
Worksheets("DATA").Cells(i, 6) = "Réseau2"
End If
If Worksheets("FORMULAIRE").CheckBox45.Value = True Then
Worksheets("DATA").Cells(i, 6) = "Réseau3"
End If
If Worksheets("FORMULAIRE").CheckBox46.Value = True Then
Worksheets("DATA").Cells(i, 7) = "OK"
End If
If Worksheets("FORMULAIRE").CheckBox47.Value = True Then
Worksheets("DATA").Cells(i, 7) = "KO"
End If
'Motifs de Refus
If Worksheets("FORMULAIRE").CheckBox1.Value = True Then
Worksheets("DATA").Cells(i, 8) = "-1"
Else
Worksheets("DATA").Cells(i, 8) = ""
End If
If Worksheets("FORMULAIRE").CheckBox2.Value = True Then
Worksheets("DATA").Cells(i, 9) = "-1"
Else
Worksheets("DATA").Cells(i, 9) = ""
End If
If Worksheets("FORMULAIRE").CheckBox3.Value = True Then
Worksheets("DATA").Cells(i, 10) = "-1"
Else
Worksheets("DATA").Cells(i, 10) = ""
End If
If Worksheets("FORMULAIRE").CheckBox27.Value = True Then
Worksheets("DATA").Cells(i, 11) = "-1"
Else
Worksheets("DATA").Cells(i, 11) = ""
End If
If Worksheets("FORMULAIRE").CheckBox18.Value = True Then
Worksheets("DATA").Cells(i, 12) = "-1"
Else
Worksheets("DATA").Cells(i, 12) = ""
End If
If Worksheets("FORMULAIRE").CheckBox22.Value = True Then
Worksheets("DATA").Cells(i, 13) = "-1"
Else
Worksheets("DATA").Cells(i, 13) = ""
End If
If Worksheets("FORMULAIRE").CheckBox5.Value = True Then
Worksheets("DATA").Cells(i, 14) = "-1"
Else
Worksheets("DATA").Cells(i, 14) = ""
End If
If Worksheets("FORMULAIRE").CheckBox6.Value = True Then
Worksheets("DATA").Cells(i, 15) = "-1"
Else
Worksheets("DATA").Cells(i, 15) = ""
End If
If Worksheets("FORMULAIRE").CheckBox7.Value = True Then
Worksheets("DATA").Cells(i, 16) = "-1"
Else
Worksheets("DATA").Cells(i, 16) = ""
End If
If Worksheets("FORMULAIRE").CheckBox19.Value = True Then
Worksheets("DATA").Cells(i, 17) = "-1"
Else
Worksheets("DATA").Cells(i, 17) = ""
End If
If Worksheets("FORMULAIRE").CheckBox8.Value = True Then
Worksheets("DATA").Cells(i, 18) = "-1"
Else
Worksheets("DATA").Cells(i, 18) = ""
End If
If Worksheets("FORMULAIRE").CheckBox28.Value = True Then
Worksheets("DATA").Cells(i, 19) = "1"
Else
Worksheets("DATA").Cells(i, 19) = ""
End If
If Worksheets("FORMULAIRE").CheckBox9.Value = True Then
Worksheets("DATA").Cells(i, 20) = "-1"
Else
Worksheets("DATA").Cells(i, 20) = ""
End If
If Worksheets("FORMULAIRE").CheckBox10.Value = True Then
Worksheets("DATA").Cells(i, 21) = "-1"
Else
Worksheets("DATA").Cells(i, 21) = ""
End If
If Worksheets("FORMULAIRE").CheckBox11.Value = True Then
Worksheets("DATA").Cells(i, 22) = "-1"
Else
Worksheets("DATA").Cells(i, 22) = ""
End If
If Worksheets("FORMULAIRE").CheckBox26.Value = True Then
Worksheets("DATA").Cells(i, 23) = "-1"
Else
Worksheets("DATA").Cells(i, 23) = ""
End If
If Worksheets("FORMULAIRE").CheckBox20.Value = True Then
Worksheets("DATA").Cells(i, 24) = "-1"
Else
Worksheets("DATA").Cells(i, 24) = ""
End If
If Worksheets("FORMULAIRE").CheckBox24.Value = True Then
Worksheets("DATA").Cells(i, 25) = "-1"
Else
Worksheets("DATA").Cells(i, 25) = ""
End If
If Worksheets("FORMULAIRE").CheckBox13.Value = True Then
Worksheets("DATA").Cells(i, 26) = "-1"
Else
Worksheets("DATA").Cells(i, 26) = ""
End If
If Worksheets("FORMULAIRE").CheckBox14.Value = True Then
Worksheets("DATA").Cells(i, 27) = "-1"
Else
Worksheets("DATA").Cells(i, 27) = ""
End If
If Worksheets("FORMULAIRE").CheckBox15.Value = True Then
Worksheets("DATA").Cells(i, 28) = "-1"
Else
Worksheets("DATA").Cells(i, 28) = ""
End If
If Worksheets("FORMULAIRE").CheckBox16.Value = True Then
Worksheets("DATA").Cells(i, 29) = "-1"
Else
Worksheets("DATA").Cells(i, 29) = ""
End If
If Worksheets("FORMULAIRE").CheckBox17.Value = True Then
Worksheets("DATA").Cells(i, 30) = "-1"
Else
Worksheets("DATA").Cells(i, 30) = ""
End If
If Worksheets("FORMULAIRE").CheckBox21.Value = True Then
Worksheets("DATA").Cells(i, 31) = "-1"
Else
Worksheets("DATA").Cells(i, 31) = ""
End If
If Worksheets("FORMULAIRE").CheckBox4.Value = True Then
Worksheets("DATA").Cells(i, 32) = "-1"
Else
Worksheets("DATA").Cells(i, 32) = ""
End If
End If
End Sub
'Bouton Aff. Nouvelle
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = Date
TextBox5.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Worksheets("FORMULAIRE").CheckBox1.Value = False
Worksheets("FORMULAIRE").CheckBox2.Value = False
Worksheets("FORMULAIRE").CheckBox3.Value = False
Worksheets("FORMULAIRE").CheckBox27.Value = False
Worksheets("FORMULAIRE").CheckBox18.Value = False
Worksheets("FORMULAIRE").CheckBox22.Value = False
Worksheets("FORMULAIRE").CheckBox5.Value = False
Worksheets("FORMULAIRE").CheckBox6.Value = False
Worksheets("FORMULAIRE").CheckBox7.Value = False
Worksheets("FORMULAIRE").CheckBox19.Value = False
Worksheets("FORMULAIRE").CheckBox8.Value = False
Worksheets("FORMULAIRE").CheckBox28.Value = False
Worksheets("FORMULAIRE").CheckBox9.Value = False
Worksheets("FORMULAIRE").CheckBox10.Value = False
Worksheets("FORMULAIRE").CheckBox11.Value = False
Worksheets("FORMULAIRE").CheckBox26.Value = False
Worksheets("FORMULAIRE").CheckBox20.Value = False
Worksheets("FORMULAIRE").CheckBox24.Value = False
Worksheets("FORMULAIRE").CheckBox13.Value = False
Worksheets("FORMULAIRE").CheckBox14.Value = False
Worksheets("FORMULAIRE").CheckBox15.Value = False
Worksheets("FORMULAIRE").CheckBox16.Value = False
Worksheets("FORMULAIRE").CheckBox17.Value = False
Worksheets("FORMULAIRE").CheckBox21.Value = False
Worksheets("FORMULAIRE").CheckBox36.Value = False
Worksheets("FORMULAIRE").CheckBox37.Value = False
Worksheets("FORMULAIRE").CheckBox38.Value = False
Worksheets("FORMULAIRE").CheckBox48.Value = False
Worksheets("FORMULAIRE").CheckBox49.Value = False
Worksheets("FORMULAIRE").CheckBox39.Value = False
Worksheets("FORMULAIRE").CheckBox40.Value = False
Worksheets("FORMULAIRE").CheckBox41.Value = False
Worksheets("FORMULAIRE").CheckBox42.Value = False
Worksheets("FORMULAIRE").CheckBox43.Value = False
Worksheets("FORMULAIRE").CheckBox44.Value = False
Worksheets("FORMULAIRE").CheckBox45.Value = False
Worksheets("FORMULAIRE").CheckBox46.Value = False
Worksheets("FORMULAIRE").CheckBox47.Value = False
Worksheets("FORMULAIRE").CheckBox4.Value = False
End Sub
'Bouton Chercher une affaire
Private Sub CommandButton3_Click()
Dim i As Integer
i = 2
Dim trouver As Boolean
trouver = False
Dim j As Integer
j = 0
While Worksheets("DATA").Cells(i, 1) <> "" And trouver = False
If Worksheets("DATA").Cells(i, 1) = TextBox1.Text Then
trouver = True
j = i
End If
If j <> 0 Then
Worksheets("FORMULAIRE").TextBox2.Text = Worksheets("DATA").Cells(i, 2)
Worksheets("FORMULAIRE").TextBox5.Text = Worksheets("DATA").Cells(i, 32)
Worksheets("FORMULAIRE").TextBox3.Text = Worksheets("DATA").Cells(i, 33)
Worksheets("FORMULAIRE").TextBox4.Text = Worksheets("DATA").Cells(i, 34)
If Worksheets("DATA").Cells(i, 8) = "-1" Then
Worksheets("FORMULAIRE").CheckBox1.Value = True
End If
If Worksheets("DATA").Cells(i, 8) = "" Then
Worksheets("FORMULAIRE").CheckBox1.Value = False
End If
If Worksheets("DATA").Cells(i, 9) = "-1" Then
Worksheets("FORMULAIRE").CheckBox2.Value = True
End If
If Worksheets("DATA").Cells(i, 9) = "" Then
Worksheets("FORMULAIRE").CheckBox2.Value = False
End If
If Worksheets("DATA").Cells(i, 10) = "-1" Then
Worksheets("FORMULAIRE").CheckBox3.Value = True
End If
If Worksheets("DATA").Cells(i, 10) = "" Then
Worksheets("FORMULAIRE").CheckBox3.Value = False
End If
If Worksheets("DATA").Cells(i, 11) = "-1" Then
Worksheets("FORMULAIRE").CheckBox27.Value = True
End If
If Worksheets("DATA").Cells(i, 11) = "" Then
Worksheets("FORMULAIRE").CheckBox27.Value = False
End If
If Worksheets("DATA").Cells(i, 12) = "-1" Then
Worksheets("FORMULAIRE").CheckBox18.Value = True
End If
If Worksheets("DATA").Cells(i, 12) = "" Then
Worksheets("FORMULAIRE").CheckBox18.Value = False
End If
If Worksheets("DATA").Cells(i, 13) = "-1" Then
Worksheets("FORMULAIRE").CheckBox22.Value = True
End If
If Worksheets("DATA").Cells(i, 13) = "" Then
Worksheets("FORMULAIRE").CheckBox22.Value = False
End If
If Worksheets("DATA").Cells(i, 14) = "-1" Then
Worksheets("FORMULAIRE").CheckBox5.Value = True
End If
If Worksheets("DATA").Cells(i, 14) = "" Then
Worksheets("FORMULAIRE").CheckBox5.Value = False
End If
If Worksheets("DATA").Cells(i, 15) = "-1" Then
Worksheets("FORMULAIRE").CheckBox6.Value = True
End If
If Worksheets("DATA").Cells(i, 15) = "" Then
Worksheets("FORMULAIRE").CheckBox6.Value = False
End If
If Worksheets("DATA").Cells(i, 16) = "-1" Then
Worksheets("FORMULAIRE").CheckBox7.Value = True
End If
If Worksheets("DATA").Cells(i, 16) = "" Then
Worksheets("FORMULAIRE").CheckBox7.Value = False
End If
If Worksheets("DATA").Cells(i, 17) = "-1" Then
Worksheets("FORMULAIRE").CheckBox19.Value = True
End If
If Worksheets("DATA").Cells(i, 17) = "" Then
Worksheets("FORMULAIRE").CheckBox19.Value = False
End If
If Worksheets("DATA").Cells(i, 18) = "-1" Then
Worksheets("FORMULAIRE").CheckBox8.Value = True
End If
If Worksheets("DATA").Cells(i, 18) = "" Then
Worksheets("FORMULAIRE").CheckBox8.Value = False
End If
If Worksheets("DATA").Cells(i, 19) = "-1" Then
Worksheets("FORMULAIRE").CheckBox28.Value = True
End If
If Worksheets("DATA").Cells(i, 19) = "" Then
Worksheets("FORMULAIRE").CheckBox28.Value = False
End If
If Worksheets("DATA").Cells(i, 20) = "-1" Then
Worksheets("FORMULAIRE").CheckBox9.Value = True
End If
If Worksheets("DATA").Cells(i, 20) = "" Then
Worksheets("FORMULAIRE").CheckBox9.Value = False
End If
If Worksheets("DATA").Cells(i, 21) = "-1" Then
Worksheets("FORMULAIRE").CheckBox10.Value = True
End If
If Worksheets("DATA").Cells(i, 21) = "" Then
Worksheets("FORMULAIRE").CheckBox10.Value = False
End If
If Worksheets("DATA").Cells(i, 22) = "-1" Then
Worksheets("FORMULAIRE").CheckBox11.Value = True
End If
If Worksheets("DATA").Cells(i, 22) = "" Then
Worksheets("FORMULAIRE").CheckBox11.Value = False
End If
If Worksheets("DATA").Cells(i, 23) = "-1" Then
Worksheets("FORMULAIRE").CheckBox26.Value = True
End If
If Worksheets("DATA").Cells(i, 23) = "" Then
Worksheets("FORMULAIRE").CheckBox26.Value = False
End If
If Worksheets("DATA").Cells(i, 24) = "-1" Then
Worksheets("FORMULAIRE").CheckBox20.Value = True
End If
If Worksheets("DATA").Cells(i, 24) = "" Then
Worksheets("FORMULAIRE").CheckBox20.Value = False
End If
If Worksheets("DATA").Cells(i, 25) = "-1" Then
Worksheets("FORMULAIRE").CheckBox18.Value = True
End If
If Worksheets("DATA").Cells(i, 25) = "" Then
Worksheets("FORMULAIRE").CheckBox18.Value = False
End If
If Worksheets("DATA").Cells(i, 26) = "-1" Then
Worksheets("FORMULAIRE").CheckBox13.Value = True
End If
If Worksheets("DATA").Cells(i, 26) = "" Then
Worksheets("FORMULAIRE").CheckBox13.Value = False
End If
If Worksheets("DATA").Cells(i, 27) = "-1" Then
Worksheets("FORMULAIRE").CheckBox14.Value = True
End If
If Worksheets("DATA").Cells(i, 27) = "" Then
Worksheets("FORMULAIRE").CheckBox14.Value = False
End If
If Worksheets("DATA").Cells(i, 28) = "-1" Then
Worksheets("FORMULAIRE").CheckBox15.Value = True
End If
If Worksheets("DATA").Cells(i, 28) = "" Then
Worksheets("FORMULAIRE").CheckBox15.Value = False
End If
If Worksheets("DATA").Cells(i, 29) = "-1" Then
Worksheets("FORMULAIRE").CheckBox16.Value = True
End If
If Worksheets("DATA").Cells(i, 29) = "" Then
Worksheets("FORMULAIRE").CheckBox16.Value = False
End If
If Worksheets("DATA").Cells(i, 30) = "-1" Then
Worksheets("FORMULAIRE").CheckBox17.Value = True
End If
If Worksheets("DATA").Cells(i, 30) = "" Then
Worksheets("FORMULAIRE").CheckBox17.Value = False
End If
If Worksheets("DATA").Cells(i, 31) = "-1" Then
Worksheets("FORMULAIRE").CheckBox21.Value = True
End If
If Worksheets("DATA").Cells(i, 31) = "" Then
Worksheets("FORMULAIRE").CheckBox21.Value = False
End If
If Worksheets("DATA").Cells(i, 32) = "-1" Then
Worksheets("FORMULAIRE").CheckBox4.Value = True
End If
If Worksheets("DATA").Cells(i, 32) = "" Then
Worksheets("FORMULAIRE").CheckBox4.Value = False
End If
If Worksheets("DATA").Cells(i, 3) = "PP" Then
Worksheets("FORMULAIRE").CheckBox36.Value = True
End If
If Worksheets("DATA").Cells(i, 3) = "CA" Then
Worksheets("FORMULAIRE").CheckBox37.Value = True
End If
If Worksheets("DATA").Cells(i, 3) = "LOA" Then
Worksheets("FORMULAIRE").CheckBox38.Value = True
End If
If Worksheets("DATA").Cells(i, 5) = "PPR" Then
Worksheets("FORMULAIRE").CheckBox39.Value = True
End If
If Worksheets("DATA").Cells(i, 5) = "CMR" Then
Worksheets("FORMULAIRE").CheckBox40.Value = True
End If
If Worksheets("DATA").Cells(i, 5) = "GE" Then
Worksheets("FORMULAIRE").CheckBox41.Value = True
End If
If Worksheets("DATA").Cells(i, 5) = "BQUE" Then
Worksheets("FORMULAIRE").CheckBox42.Value = True
End If
If Worksheets("DATA").Cells(i, 6) = "Réseau1" Then
Worksheets("FORMULAIRE").CheckBox43.Value = True
End If
If Worksheets("DATA").Cells(i, 6) = "Réseau2" Then
Worksheets("FORMULAIRE").CheckBox44.Value = True
End If
If Worksheets("DATA").Cells(i, 6) = "Réseau3" Then
Worksheets("FORMULAIRE").CheckBox45.Value = True
End If
If Worksheets("DATA").Cells(i, 7) = "OK" Then
Worksheets("FORMULAIRE").CheckBox46.Value = True
End If
If Worksheets("DATA").Cells(i, 7) = "KO" Then
Worksheets("FORMULAIRE").CheckBox47.Value = True
End If
If Worksheets("DATA").Cells(i, 4) = "PP" Then
Worksheets("FORMULAIRE").CheckBox48.Value = True
End If
If Worksheets("DATA").Cells(i, 4) = "PM" Then
Worksheets("FORMULAIRE").CheckBox48.Value = True
End If
End If
i = i + 1
Wend
End Sub
Bonjour,
Une copie de ton classeur débarrassé de tes datas mais avec juste un ou 2 enregistrements bidon auraient été plus utile :
Il est possible que ma démo tombe un peu à coté...
Question1 : voir en pièce jointe
Question2 : voir en pièce jointe
A+
Bonjour bonjour !
Merci galopin pour ta réponse, ça a en fait réglé une bonne partie de mon problème.
Sinon, je n'ai pas bien saisi la partie modification
Dans mon formulaire, une fois je clique sur enregistrer, le numéro d'affaire et ses caractéristiques sont enregistrées sur une ligne d'une feuille Excel.
Normalement, une affaire ne doit être présente qu'une seule fois, ce qui fait que j'ai introduit un msgbox pour dire à l'utilisateur que cette affaire est déjà enregistrée.
Cependant, il peut m'arriver de m'être trompée ... et donc de vouloir modifier l'existant. Et c là que je bloque
Bonjour,
Mettre une copie de ton classeur débarrassé de tes datas mais avec juste un ou 2 enregistrements bidon.
A+
Voilààà
Bonsoir,
Oui c'est bien, mon code est plus compliqué mais adapté à d'autres situations.
Ton code est correct. Quel est le problème. ?
Laisser le choix à l'utilisateur d'effectuer une modification ou non ? ou au contraire modifier d'office l'existant ?
A+
Dans mon code, une fois j'essaie d'enregistrer un numéro d'affaire qui existe déjà sur la table DATA, un msgbox me dit qu'il s'agit d'une affaire déjà enregistrée.
Mon problème est que parfois, je désire modifier l'existant, par exemple, pour l'affaire 73166270 sur la feuille DATA, j'avais coché CA, que je désire désormais changer en PP, et je ne veux plus cocher le budget disponible. Bref, je veux pouvoir modifier ce que j'ai déjà coché
Ah, j'avais pas bien lu ta phrase
En effet, je désire laisser le choix à l'utilisateur de modifier ou pas.
Bonsoir,
Pour rester un peu dans ton esprit... le code complété.
A+
Merci beaucoup beaucoup beaucoup !
C exactement ce dont j'avais besoin !
Mon autre petit soucis.... si je peux....
J'ai des TEXTBOX, COMBOBOX et CHECKBOX..... et, du coup, j'ai une erreur sur le code ci-dessous car je ne fais référence qu'à des TEXTBOX (voir sur mon fichier). Comment puis-je faire ?
For k = 1 To Ncol
Next k
UN SUPER SUPER GRAND MERCI.