Userform excel : modifier les données sur la bonne feuille

Résolu
coraliie.c Messages postés 22 Statut Membre -  
pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

J'ai réussi tant bien que mal à programmer un userform sur Excel.
J'ai mis un bouton sur une feuille pour lancer le formulaire.
Jusque là tout va bien mais les choses se compliquent (forcément !) puisque mon bouton est sur une feuille et mon tableau de données sur une 2ème feuille.
J'ai programmé sur mon userform un bouton modifier et un bouton ajouter mais lorsque je clique dessus, les modifications et/ou ajouts se font sur la feuille 1 (ou il y a le bouton) et non sur la feuille 2 (ou il y a le tableau de données).

Comment faire pour que les modifs et ajouts se fassent sur la bonne feuille ?

Voici mes codes :

'Pour le bouton Ajouter
Private Sub CommandButton1_Click()
Dim L As Integer
If MsgBox("Confirmez-vous l'insertion ?", vbYesNo, "Confirmation d'insertion") = vbYes Then
L = Sheets("Habilitation Agent").Range("a65536").End(xlUp).Row + 1
Range("B" & L).Value = ComboBox1
Range("C" & L).Value = TextBox2
Range("D" & L).Value = TextBox3
Range("L" & L).Value = TextBox4
Range("T" & L).Value = TextBox5
Range("AB" & L).Value = TextBox6
Range("AJ" & L).Value = TextBox7
Range("AR" & L).Value = TextBox8
Range("AZ" & L).Value = TextBox9
Range("G" & L).Value = TextBox10
Range("O" & L).Value = TextBox11
Range("W" & L).Value = TextBox12
Range("AE" & L).Value = TextBox13
Range("AM" & L).Value = TextBox14
Range("AU" & L).Value = TextBox15
Range("BC" & L).Value = TextBox16
Range("H" & L).Value = TextBox17
Range("P" & L).Value = TextBox18
Range("X" & L).Value = TextBox19
Range("AF" & L).Value = TextBox20
Range("AN" & L).Value = TextBox21
Range("AV" & L).Value = TextBox22
Range("BD" & L).Value = TextBox23
Range("F" & L).Value = TextBox24
Range("N" & L).Value = TextBox25
Range("V" & L).Value = TextBox26
Range("AD" & L).Value = TextBox27
Range("AL" & L).Value = TextBox28
Range("AT" & L).Value = TextBox29
Range("BB" & L).Value = TextBox30
End If
End Sub

'Pour le bouton Modifier
Private Sub CommandButton2_Click()
Dim Ligne As Long
Dim I As Integer
If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then Exit Sub
Ligne = Me.ComboBox1.ListIndex + 2
Range("B" & Ligne) = Me.ComboBox1
Range("C" & Ligne) = TextBox2
Range("D" & Ligne) = TextBox3
Range("L" & Ligne) = TextBox4
Range("T" & Ligne) = TextBox5
Range("AB" & Ligne) = TextBox6
Range("AJ" & Ligne) = TextBox7
Range("AR" & Ligne) = TextBox8
Range("AZ" & Ligne) = TextBox9
Range("G" & Ligne) = TextBox10
Range("O" & Ligne) = TextBox11
Range("W" & Ligne) = TextBox12
Range("AE" & Ligne) = TextBox13
Range("AM" & Ligne) = TextBox14
Range("AU" & Ligne) = TextBox15
Range("BC" & Ligne) = TextBox16
Range("F" & Ligne) = TextBox24
Range("N" & Ligne) = TextBox25
Range("V" & Ligne) = TextBox26
Range("AD" & Ligne) = TextBox27
Range("AL" & Ligne) = TextBox28
Range("AT" & Ligne) = TextBox29
Range("BB" & Ligne) = TextBox30
End Sub

1 réponse

  1. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773
     
    Bonjour,

    Deux choix :
    1- vous référencez systématiquement le nom de la feuille :

    'Pour le bouton Ajouter
    Private Sub CommandButton1_Click()
    Dim L As Integer
    If MsgBox("Confirmez-vous l'insertion ?", vbYesNo, "Confirmation d'insertion") = vbYes Then
    L = Sheets("Habilitation Agent").Range("a65536").End(xlUp).Row + 1
    Sheets("Feuil2").Range("B" & L).Value = ComboBox1
    Sheets("Feuil2").Range("C" & L).Value = TextBox2
    Sheets("Feuil2").Range("D" & L).Value = TextBox3
    Sheets("Feuil2").Range("L" & L).Value = TextBox4
    Sheets("Feuil2").Range("T" & L).Value = TextBox5
     'Etc....


    2- Vous utilisez un bloc With End With :
    'Pour le bouton Ajouter
    Private Sub CommandButton1_Click()
    Dim L As Integer
    If MsgBox("Confirmez-vous l'insertion ?", vbYesNo, "Confirmation d'insertion") = vbYes Then
        L = Sheets("Habilitation Agent").Range("a65536").End(xlUp).Row + 1
        With Sheets("Feuil2")
            .Range("B" & L).Value = ComboBox1
            .Range("C" & L).Value = TextBox2
            .Range("D" & L).Value = TextBox3
            .Range("L" & L).Value = TextBox4
            .Range("T" & L).Value = TextBox5
             'Etc....
            .Range("AL" & L).Value = TextBox28
            .Range("AT" & L).Value = TextBox29
            .Range("BB" & L).Value = TextBox30
        End With
    End If
    End Sub
    1
    1. coraliie.c Messages postés 22 Statut Membre
       
      Super la première solution marche !

      Merci beaucoup pour ton aide
      0
      1. pijaku Messages postés 13513 Date d'inscription   Statut Modérateur Dernière intervention   2 773 > coraliie.c Messages postés 22 Statut Membre
         
        de rien.
        A+
        0