'patricktoulon
'quasiment tout les formats de date sont détectés
'dd/mm/yyyy
'dd -mm - yyyy
'dd mm yyyy ? ? espace
'yyyy.mm.dd
'd m yy
'd -mmm - yyyy
'yyyy , mm, dd
'dd mmm (date partielle)
'mmm yyyy
'etc....
'etc....
Sub test()
Cells(1, 1) = "Toto"
Cells(1, 1).NumberFormat = "dd/mm/yyyy"
MsgBox Cells(1, 1).Text & vbCrLf & Cells(1, 1).NumberFormat & " : " & EstFormatDate(Cells(1, 1)) & vbCrLf & " est une date :" & IsDate(Cells(1, 1))
Cells(2, 1).NumberFormat = "@"
Cells(2, 1) = "06/07/2025"
MsgBox Cells(2, 1).Text & vbCrLf & Cells(2, 1).NumberFormat & " : " & EstFormatDate(Cells(2, 1)) & vbCrLf & " est une date :" & IsDate(Cells(2, 1))
Cells(3, 1).NumberFormat = "General"
Cells(3, 1) = "07/07/2025"
MsgBox Cells(3, 1).Text & vbCrLf & Cells(3, 1).NumberFormat & " : " & EstFormatDate(Cells(3, 1)) & vbCrLf & " est une date :" & IsDate(Cells(3, 1))
End Sub
Function EstFormatDate(cel As Range) As Boolean
Dim nbF As String
nbF = LCase(cel.NumberFormat)
'suppression des caractères genants utilisé éventuellement dans le pattern
' sauf le separateur de date bien sur!!!, lui on l'échappe dans le pattern
nbF = Replace(Replace(Replace(nbF, """", ""), "$", ""), "€", "")
With CreateObject("VBScript.RegExp")
.IgnoreCase = True
.Global = False
' On cherche au moins deux composantes (d, m, y) parmi les trois, séparées par des séparateurs
.Pattern = "\b(?:d{1,4}|m{1,4}|y{2,4})([\s\/\-,\.]*)" & _
"(?:d{1,4}|m{1,4}|y{2,4})(\1?(?:d{1,4}|m{1,4}|y{2,4}))?\b"
EstFormatDate = .test(nbF)
End With
End Function