Option Explicit
'sub privée Job() : appelée uniquement par ces 2 subs : CommandButton1_Click()
'et CommandButton3_Click() (donc par un clic / boutons Ajouter et Modifier)
Private Sub Job(lig&)
With Cells(lig, 2) 'on référence la colonne B : Appart
.Value = TextBox1 'Appart
.Offset(, 1) = ComboBox1 'code locataire
.Offset(, 2) = TextBox2 'Genre
.Offset(, 3) = TextBox3 'Nom
.Offset(, 4) = TextBox4 'Prénom
.Offset(, 6) = TextBox5 'Tel / Mail
.Offset(, 7) = TextBox6 'entrée le
End With
End Sub
'clic / bouton "Ajouter"
Private Sub CommandButton1_Click()
If ComboBox1 = "" Then MsgBox "Veuillez renseigner le champ 'Nom/Prénom'": Exit Sub
If MsgBox("Confirmez-vous l'ajout des données ?", 4, "confirmation") <> 6 Then Exit Sub
Application.ScreenUpdating = 0: Feuil1.Select
Dim lig&: lig = Cells(Rows.Count, 2).End(3).Row + 1 'selon colonne B : Appart
Job lig: Unload UserForm1: UserForm1.Show
End Sub
'clic / bouton "Rechercher"
Private Sub CommandButton2_Click()
If ComboBox1 = "" Then Exit Sub
Dim lig&: lig = ComboBox1.ListIndex + 2: Application.ScreenUpdating = 0
With Cells(lig, 1)
TextBox1 = .Value 'Appart
ComboBox1 = .Offset(, 1) 'code locataire
TextBox2 = .Offset(, 2) 'Genre
TextBox3 = .Offset(, 3) 'Nom
TextBox4 = .Offset(, 4) 'Prénom
TextBox5 = .Offset(, 6) 'Tel / Mail
TextBox6 = .Offset(, 7) 'entrée le
End With
End Sub
'clic / bouton "Modifier"
Private Sub CommandButton3_Click()
Dim lig&: Application.ScreenUpdating = 0: Feuil1.Select
If ComboBox1 <> "" Then
lig = ComboBox1.ListIndex + 2: Job lig
Application.ScreenUpdating = -1: MsgBox "Modification effectuée"
Else
Application.ScreenUpdating = -1
MsgBox "Veuillez sélectionner le Nom/Prénom de la personne à modifier"
Exit Sub
End If
Unload UserForm1: UserForm1.Show 0
End Sub