Modification dans un formulaire

Hello la communauté,

Dans le formulaire que j'ai créé, j'aimerai offrir la possibilité de mettre à jour des données. Le problème que je rencontre est à ce niveau là.

Lorsque je modifie une donnée d'une personne, toutes les données sont modifiées.

Ce n'est évidemment pas ce que je recherche!

Je suis un peu perdu là...

rivate Sub CdeMiseAJour_Click()

     Dim x As Long
        Dim y As Long
        x = Sheets("feuille matricule").Range("A" & Rows.Count).End(xlUp).Row
        For y = 5 To x

'****************************************************************************
    'Validation
'*****************************************************************************

    If Me.CbxCivilite.Value = "" Then
    MsgBox "Vueillez sélectionner la civilité du MDP", vbCritical
    Exit Sub
    End If

    If IsNumeric(Me.TxtNNational.Value) = False Then
    MsgBox "Veuillez introduire le numéro national du MDP", vbCritical
    Exit Sub
    End If

    If Me.CbxEtatCivil.Value = "" Then
    MsgBox "Vueillez sélectionner la situation familliale du MDP", vbCritical
    Exit Sub
    End If

    If Me.CbxEnfantACharge.Value = "" Then
    MsgBox "Vueillez sélectionner si l'enfant est à charge ou non du MDP", vbCritical
    Exit Sub
    End If

    If Me.CbxDenominationDiplome.Value = "" Then
    MsgBox "Vueillez sélectionner la dénomination du diplôme du MDP", vbCritical
    Exit Sub
    End If

    If Me.CbxNiveauDiplome.Value = "" Then
    MsgBox "Vueillez sélectionner le niveau de diplôme du MDP", vbCritical
    Exit Sub

    End If

        If Sheets("feuille matricule").Cells(y, 1).Value = TxtChercher.Text Then
        Sheets("feuille matricule").Cells(y, 1).Value = CbxCivilite
        Sheets("feuille matricule").Cells(y, 2).Value = TxtNom
        Sheets("feuille matricule").Cells(y, 3).Value = TxtPrenom
        Sheets("feuille matricule").Cells(y, 4).Value = TxtNNational
        Sheets("feuille matricule").Cells(y, 5).Value = TxtNMatricule
        Sheets("feuille matricule").Cells(y, 6).Value = TxtEmail
        Sheets("feuille matricule").Cells(y, 7).Value = TxtGSM
        Sheets("feuille matricule").Cells(y, 8).Value = TxtTelephone
        Sheets("feuille matricule").Cells(y, 9).Value = TxtNUrgence
        Sheets("feuille matricule").Cells(y, 10).Value = TxtEntreeEnFonction
        Sheets("feuille matricule").Cells(y, 11).Value = TxtNCompteBancaire
        Sheets("feuille matricule").Cells(y, 12).Value = TxtDateNaissance
        Sheets("feuille matricule").Cells(y, 14).Value = TxtLieuNaissance
        Sheets("feuille matricule").Cells(y, 15).Value = TxtPaysNaissance
        Sheets("feuille matricule").Cells(y, 16).Value = TxtAdresse
        Sheets("feuille matricule").Cells(y, 17).Value = TxtNumero
        Sheets("feuille matricule").Cells(y, 18).Value = TxtBoite
        Sheets("feuille matricule").Cells(y, 19).Value = TxtCodePostal
        Sheets("feuille matricule").Cells(y, 20).Value = TxtCommune
        Sheets("feuille matricule").Cells(y, 21).Value = CbxEtatCivil
        Sheets("feuille matricule").Cells(y, 22).Value = TxtNomConjoint
        Sheets("feuille matricule").Cells(y, 23).Value = TxtPrenomConjoint
        Sheets("feuille matricule").Cells(y, 24).Value = TxtPaysNaissanceConjoint
        Sheets("feuille matricule").Cells(y, 25).Value = TxtDateNaissanceConjoint
        Sheets("feuille matricule").Cells(y, 26).Value = TxtLieuNaissanceConjoint
        Sheets("feuille matricule").Cells(y, 27).Value = TxtGSMConjoint
        Sheets("feuille matricule").Cells(y, 28).Value = TxtCombienEnfants
        Sheets("feuille matricule").Cells(y, 29).Value = CbxEnfantACharge
        Sheets("feuille matricule").Cells(y, 30).Value = CbxNiveauDiplome
        Sheets("feuille matricule").Cells(y, 31).Value = CbxDenominationDiplome
        Sheets("feuille matricule").Cells(y, 32).Value = TxtDateDiplome
        Sheets("feuille matricule").Cells(y, 33).Value = TxtEcoleDiplome
        Sheets("feuille matricule").Cells(y, 34).Value = TxtDatePrestation

        End If
        Next y
        

Bonjour,

C'est normal car tu fais une boucle. Plutôt que de boucler sur toutes tes lignes et faire les modifications sur chaque ligne, il te faut trouver la ligne sur laquelle tu veux mettre les valeurs à jour.

PS : c'est "veuillez" et non "vueillez"

En plus, tu as du code fonctionnel sur d'autres boutons. Par exemple, ton bouton supprimer fonctionne bien pour trouver la bonne ligne. Tu as le code suivant...

Private Sub CdeSupprimer_Click()

Dim x As Long
Dim y As Long
    x = Sheets("feuille matricule").Range("A" & Rows.Count).End(xlUp).Row
    For y = 5 To x
        If Sheets("feuille matricule").Cells(y, 1).Value = TxtChercher.Text Then
        Rows(y).Delete

        End If
    Next y

Tu pourrais passer par une fonction match ou toute autre solution permettant de trouver une correspondance, mais on peut partir de ton code et juste l'adapter un peu. Il faudrait trouver une donnée qui est forcément unique. Genre le numéro national j'imagine ?

Private Sub CdeMiseAJour_Click()

        Dim x As Long
        Dim z As Long
        x = Sheets("feuille matricule").Range("A" & Rows.Count).End(xlUp).Row
        For z = 5 To x
        If Format(Sheets("feuille matricule").Cells(z, 4).Value, "00000000000") = Format(TxtNNational, "00000000000") Then
        y = z
        End If
        Next z

Merci JoyeuxNoel pour ton aide.

Je vais tester ça tout de suite.

Bonjour à tous,

Une autre façon de faire avec un tableau structuré indexé et une table de correspondance entre les champs du tableau et les contrôles du userform.

Il n'est peut-être pas utile de tout mettre dans la listbox du userform.

Hello Eric Kergresse,

Un tout grand merci pour la solution proposé!
C'est très chouette que tu aies contribué à mon tableau.

Je ne comprends pas ce que signifie l'index que tu as mis dans l'userform. Quel est l'usage que je peux en faire?

C'est pour identifier la ligne à mettre à jour, car dans ton fonctionnement d'origine tu peux modifier tous les champs , donc tu ne peux pas garantir que la ligne va être mise à jour.

Rechercher des sujets similaires à "modification formulaire"