'Pour le formulaire
Private Sub UserForm_Initialize()
Dim J As Long
Dim I As Integer
Me.ComboBox2.ColumnCount = 1 'Pour la liste déroulante Civilité
Me.ComboBox2.List = Array("", "M.", "Mme", "Mlle")
Me.ComboBox2.Listidex = 0
Dim Ws As Worksheet
Set Ws = Sheets("fEUIL1") 'Correspond au nom de votre onglet dans le fichier Excel
With Me.ComboBox1
For J = 2 To Ws.Range("A" & Rows.Count).End(xlUp).Row
.AddItem Ws.Range("A" & J)
Next J
End With
For I = 1 To 7
Me.Controls("TextBox" & I).Visible = True
Next I
End Sub
'Pour la liste déroulante Code client
Private Sub ComboBox1_Change()
Dim Ligne As Long
Dim I As Integer
'Manque ci-dessous 2 lignes Corrigé
Dim Ws As Worksheet
Set Ws = Sheets("fEUIL1") 'Correspond au nom de votre onglet dans le fichier Excel
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
Ligne = Me.ComboBox1.ListIndex + 2
Me.ComboBox2 = Ws.Cells(Ligne, "B")
For I = 1 To 7
Me.Controls("TextBox" & I) = Ws.Cells(Ligne, I + 2).Value
Next I
End Sub
'Pour le bouton Nouveau contact
Private Sub CommandButton1_Click()
Dim L As Integer
'Manque ci-dessous 2 lignes Corrigé
Dim Ws As Worksheet
Set Ws = Sheets("fEUIL1") 'Correspond au nom de votre onglet dans le fichier Excel
If MsgBox("Confirmez-vous l'insertion de ce nouveau contact ?", vbYesNo, "Demande de confirmation d'ajout") = vbYes Then
' Pas de feuille Clients dans ce fichier excel
L = Sheets("Clients").Range("a65536").End(xlUp).Row + 1 'Pour placer le nouvel enregistrement à la première ligne de tableau non vide
Ws.Range("A" & L).Value = Me.ComboBox1.Value
Ws.Range("B" & L).Value = Me.ComboBox2.Value
Ws.Range("C" & L).Value = Me.TextBox1.Value
Ws.Range("D" & L).Value = Me.TextBox2.Value
Ws.Range("E" & L).Value = Me.TextBox3.Value
Ws.Range("F" & L).Value = Me.TextBox4.Value
Ws.Range("G" & L).Value = Me.TextBox5.Value
Ws.Range("H" & L).Value = Me.TextBox6.Value
Ws.Range("I" & L).Value = Me.TextBox7.Value
End If
End Sub
'Pour le bouton Modifier
Private Sub CommandButton2_Click()
Dim Ligne As Long
Dim I As Integer
'Manque ci-dessous 2 lignes Corrigé
Dim Ws As Worksheet
Set Ws = Sheets("fEUIL1") 'Correspond au nom de votre onglet dans le fichier Excel
If MsgBox("Confirmez-vous la modification de ce contact ?", vbYesNo, "Demande de confirmation de modification") = vbYes Then
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
Ligne = Me.ComboBox1.ListIndex + 2
Ws.Cells(Ligne, "B") = Me.ComboBox2.Value
For I = 1 To 7
If Me.Controls("TextBox" & I).Visible = True Then
Ws.Cells(Ligne, I + 2) = Me.Controls("TextBox" & I).Value
End If
Next I
End If
End Sub
'Pour le bouton Quitter
Private Sub CommandButton3_Click()
Unload Me
End Sub