Public OK As Boolean
Private Sub CommandButton1_Click()
Controle
If Not OK Then
MsgBox "La date saisie n'est pas valide"
Exit Sub
End If
Sheets("Feuil1").Range("I3") = CDate(TextBox1)
Unload Me
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Val As Byte
'Limiter le nb caracteres maxi dans textbox
TextBox1.MaxLength = 5
'Mettre un slash automatiquement 00/00/0000
Val = Len(TextBox1)
If Val = 2 Then TextBox1 = TextBox1 & "/"
If KeyCode = 13 Then
Controle
If OK Then
Sheets("Feuil1").Range("I3") = CDate(TextBox1)
Unload Me
Else
Exit Sub
End If
End If
End Sub
Sub Controle()
Dim Mois As Integer, Annee As Integer, Pos As String
OK = False
Pos = IIf(TextBox1 <> "", Mid(TextBox1, 3, 1), "*")
If Pos <> "/" Then GoTo invalide
If Len(TextBox1) < 5 Then GoTo invalide
Mois = CInt(Left(TextBox1, 2))
If Mois < 1 Or Mois > 12 Then GoTo invalide
Annee = CInt(Right(TextBox1, 2))
If Annee < 20 Or Annee > 50 Then GoTo invalide
OK = True
Exit Sub
invalide:
Application.EnableEvents = False
TextBox1.Value = ""
MsgBox "La date saisie n'est pas valide"
Application.EnableEvents = True
End Sub
Private Sub UserForm_Activate()
OK = False
End Sub