Private Sub Worksheet_Change(ByVal Target As Excel.Range)
' d'après la macro originale de Chip Pearson
'******* [url=http://www.cpearson.com/Excel/DateTimeEntry.htm]Date And Time Entry[/url]
Dim DateStr As String
'--------------------------------
On Error GoTo EndMacro
' ********** *********** adapter la plage selon besoins
If Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
Exit Sub
End If
'--------------------------------
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
'----------------------------------
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Formula)
Case 4 ' e.g., j/m/aa
DateStr = Left(.Formula, 1) & "/" & _
Mid(.Formula, 2, 1) & "/" & Right(.Formula, 2)
'**********************************************
Case 5 ' e.g., j/mm/aa !!! ### !!! 19398 (jjmaa) ==> 20398 (jmmaa) (suite logique)
If Mid(.Formula, 2, 2) > "12" Then
'21398 21 mars 1998
DateStr = Left(.Formula, 2) & "/" & Mid(.Formula, 3, 1) & "/" & Right(.Formula, 2)
Else
'20398 2 mars 1998
DateStr = Left(.Formula, 1) & "/" & Mid(.Formula, 2, 2) & "/" & Right(.Formula, 2)
End If
'**********************************************
Case 6 ' e.g., jj/mm/aa = 2-Sep-1998
DateStr = Left(.Formula, 2) & "/" & _
Mid(.Formula, 3, 2) & "/" & Right(.Formula, 2)
Case 7 ' e.g., j/mm/aaaa = 23-Jan-1998 NOT 3-Dec-1998
DateStr = Left(.Formula, 1) & "/" & _
Mid(.Formula, 2, 2) & "/" & Right(.Formula, 4)
Case 8 ' e.g., jj/mm/aaaa = 2-Sep-1998
DateStr = Left(.Formula, 2) & "/" & _
Mid(.Formula, 3, 2) & "/" & Right(.Formula, 4)
Case Else
Err.Raise 0
End Select
.Formula = DateValue(DateStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "La date saisie " _
& Chr(10) & CLng(Target.Value) _
& Chr(10) & "n'est pas valide !" _
& Chr(10) & "4 car. => jmaa" _
& Chr(10) & "5 car. => jmmaa" _
& Chr(10) & "6 car. => jjmmaa" _
& Chr(10) & "7 car. => jmmaaaa" _
& Chr(10) & "8 car. => jjmmaaaa"
Application.EnableEvents = True
End Sub