Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" And TextBox2.Text <> "" Then DiffDate
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" And TextBox2.Text <> "" Then DiffDate
End Sub
Private Sub DiffDate()
Dim DateDebut As Date
Dim DateFin As Date
'gère l'erreur de la date entrée de cette façon : dimanche 24 février 2013
On Error Resume Next
DateDebut = CDate(TextBox1.Value)
'reconstitue la date de façon à ce que le compilateur puisse la reconnaître
If Err.Number <> 0 Then DateDebut = Deformater(TextBox1.Value)
'remet à 0 pour la date suivante
Err.Number = 0
DateFin = CDate(TextBox2.Value)
'idem que plus haut
If Err.Number <> 0 Then DateFin = Deformater(TextBox2.Value)
On Error GoTo 0 'supprime le gestionnaire
On Error GoTo Fin 'remet en place un gestionnaire pour gérer l'erreur du textbox par encore rempli
TextBox3 = DateDiff("d", DateDebut, DateFin, 2, 2) & " jours"
TextBox4 = DateDiff("m", DateDebut, DateFin, 2, 2) & " Mois"
TextBox5 = DateDiff("ww", DateDebut, DateFin, 2, 2) & " Semaine(s)"
TextBox6 = DateDiff("q", DateDebut, DateFin, 2, 2) & " Trimestre(s)"
TextBox7 = DateDiff("yyyy", DateDebut, DateFin, 2, 2) & " Année(s)"
TextBox8 = DateDiff("h", DateDebut, DateFin, 2, 2) & " Heure(s)"
TextBox9 = DateDiff("n", DateDebut, DateFin, 2, 2) & " Minutes(s)"
TextBox10 = DateDiff("s", DateDebut, DateFin, 2, 2) & " Seconde(s)"
Fin:
End Sub
Function Deformater(LaDate As String) As Date
Dim Jour As Long
Dim Mois As String
Dim Annee As Long
Select Case LCase(Split(LaDate, " ")(2))
Case "janvier": Mois = 1
Case "février": Mois = 2
Case "mars": Mois = 3
Case "avril": Mois = 4
Case "mai": Mois = 5
Case "juin": Mois = 6
Case "juillet": Mois = 7
Case "août": Mois = 8
Case "septembre": Mois = 9
Case "octobre": Mois = 10
Case "novembre": Mois = 11
Case "décembre": Mois = 12
End Select
Jour = Split(LaDate, " ")(1)
Annee = Split(LaDate, " ")(3)
Deformater = CDate(Jour & "/" & Mois & "/" & Annee)
End Function