Dim P As Range 'mémorise la variable
Private Sub CommandButton1_Click() 'Ajouter
If TextBox3 = "" Then TextBox3.SetFocus: Exit Sub
If Application.CountIf(P.Columns(1), TextBox3) Then MsgBox "'" & TextBox3 & "' existe déjà...": Exit Sub
P(P.Rows.Count + 1, 1) = TextBox3
P(P.Rows.Count + 1, 2) = TextBox4
UserForm_Initialize
End Sub
Private Sub CommandButton2_Click() 'Modifier
If ListBox3.ListIndex = -1 Then MsgBox "Sélectionnez une ligne...": Exit Sub
If TextBox3 = "" Then TextBox3.SetFocus: Exit Sub
P(ListBox3.ListIndex + 2, 1) = TextBox3
P(ListBox3.ListIndex + 2, 2) = TextBox4
UserForm_Initialize
End Sub
Private Sub CommandButton3_Click() 'Supprimer
If ListBox3.ListIndex = -1 Then MsgBox "Sélectionnez une ligne...": Exit Sub
If MsgBox("Supprimer '" & ListBox3 & "' ?", 4) = 7 Then Exit Sub
P.Rows(ListBox3.ListIndex + 2).Delete xlUp
UserForm_Initialize
End Sub
Private Sub ListBox3_Click()
TextBox3 = ListBox3.Column(0)
TextBox4 = ListBox3.Column(1)
End Sub
Private Sub UserForm_Initialize()
Set P = Sheets("AMRI").[A1].CurrentRegion
If P.Rows.Count = 1 Then ListBox3.Clear: CommandButton2.Enabled = False: CommandButton3.Enabled = False: Exit Sub
P.Sort P(1), xlAscending, Header:=xlYes 'tri alphabétique
ListBox3.List = P.Rows(2).Resize(P.Rows.Count - 1, 2).Value
End Sub