Dans l'USF eleves, modifiez comme suit :
1. Dans la Private Sub UserForm_Initialize() : ajoutez ces lignes juste avant le END SUB
TextBox1 = WorksheetFunction.Max(Worksheets("BDD").Range("A:A")) + 1
TextBox2.SetFocus
2. Private Sub btnEnreg_Click(), remplacez le code complet par celui ci-dessous
Private Sub btnEnreg_Click()
Dim x As Integer, ws As Worksheet
Set ws = Sheets("BDD")
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
' ENREGISTRER
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
' Si rien saisit en nom, la macro s'arrète
If TextBox1 = vbNullString Then
MsgBox "Veuillez entrer le matricule.", vbInformation, "Matricule"
Exit Sub
ElseIf TextBox2 = vbNullString Then
MsgBox "Veuillez entrer le nom.", vbInformation, "Nom élève"
Exit Sub
' Si rien saisit en prénom, la macro s'arrète
ElseIf TextBox3 = vbNullString Then
MsgBox "Veuillez entrer un prénom.", vbInformation, "Prénom élève"
Exit Sub
' Si rien saisit en matière, la macro s'arrète
ElseIf ComboBox1 = vbNullString Then
MsgBox "Veuillez entrer la Matiére.", vbInformation, "Matière"
Exit Sub
' Si rien saisit en date naissance, la macro s'arrète
ElseIf TextBox2 = vbNullString Then
MsgBox "Veuillez entrer une date de naissance.", vbInformation, "Date Naissance"
Exit Sub
' Si rien choisit en classe, la macro s'arrète
ElseIf ComboBox3 = vbNullString Then
MsgBox "Veuillez entrer la classe.", vbInformation, "Classe"
Exit Sub
' Si rien choisit en professeur, la macro s'arrète
ElseIf ComboBox4 = vbNullString Then
MsgBox "Veuillez entrer un professeur.", vbInformation, "Professeur"
Exit Sub
End If
With ws
x = .Range("A" & Rows.Count).End(xlUp).Row + 1 'enregistrement des infos aprés la dernière ligne
.Cells(x, 1).Value = TextBox1.Text 'matricule
.Cells(x, 2).Value = TextBox2.Text 'nom
.Cells(x, 3).Value = CDate(TextBox3.Value) 'date naissance
.Cells(x, 3).NumberFormat = "m/d/yyyy"
.Cells(x, 4).Value = ComboBox1.Text 'Matiére
.Cells(x, 5).Value = TextBox4.Value 'paiment
.Cells(x, 6).Value = ComboBox2.Text 'classe
.Cells(x, 7).Value = ComboBox3.Text 'groupe
.Cells(x, 8).Value = ComboBox4.Text 'prof
End With
'bordures
With Range("A" & x & ":H" & x) '.Select
.BorderAround LineStyle:=xlContinuous
.BorderAround Weight:=xlThin
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End With
'annonce
MsgBox "Enregistré", , "confirmation enregistrement"
Call Effacer
TextBox1 = WorksheetFunction.Max(Worksheets("BDD").Range("A:A")) + 1
End Sub
3. Ajoutez ce code pour l'effacement des données dans l'USF après le clic sur le bouton Enregistrer
Private Sub Effacer()
Dim c As Control
For Each c In Me.Controls
Select Case TypeName(c)
Case "TextBox"
c.Value = vbNullString
Case "ComboBox"
c.Value = vbNullString
c.ListIndex = -1
End Select
Next c
Me.ListBox1.Clear
End Sub
Refaites un test avant d'aller plus loin