Option Explicit
Function Age_Anniversaire_dans(ByVal dat1 As Variant, Optional ByVal dat2 As Variant = 0, Optional ByVal A_ou_M_ou_J As Long = 1)
'auteur:patricktoulon sur Exceldownloads
'date 03/02/2023
' là aussi je décale les dates de +100 ans pour avoir la possibilité d'aller jusqu'à 1800
'*************************************
Dim A$, M&, J$, Dtemp$, anniv As Date, dtemps As Date, Maint As Date
If Val(dat2) = 0 Then dat2 = CStr(Date)
dat1 = CDate(Left(dat1, 6) & Val(Right(dat1, 4) + 100))
dat2 = CDate(Left(dat2, 6) & Val(Right(dat2, 4) + 100))
If dat1 > dat2 Then dtemps = dat2: dat2 = dat1: dat1 = dtemps
A = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""y"")")
Maint = DateSerial(Year(Date) + 100, Month(Date), Day(Date))
anniv = CDate(Left(dat1, 6) & Year(Date) + 100)
If anniv < Maint Then dtemps = anniv: anniv = Maint: Maint = dtemps
M = Evaluate("=DATEDIF(" & CLng(Maint) & "," & CLng(anniv) & ",""ym"")")
J = Evaluate("=DATEDIF(" & CLng(DateSerial(Year(Date) + 100, Month(Date), Day(Date))) & "," & CLng(anniv) & ",""md"")")
If M = 1 And Month(anniv) > M Then M = 12 - M
' Debug.Print dat1 & vbCrLf & dat2 & vbCrLf & A & vbCrLf & M & " mois " & J & " jours"
Age_Anniversaire_dans = Array(" ", A, M, J)(A_ou_M_ou_J)
End Function
Option Explicit
Function Age_Anniversaire_dans(ByVal dat1 As Variant, Optional ByVal dat2 As Variant = 0, Optional ByVal A_ou_M_ou_J As Long = 1)
'auteur:patricktoulon sur Exceldownloads
'date 03/02/2023
' là aussi je décale les dates de +100 ans pour avoir la possibilité d'aller jusqu'à 1800
'*************************************
Dim A$, M&, J$, Dtemp$, anniv As Date, dtemps As Date, Maint As Date
If Val(dat2) = 0 Then dat2 = Date
dat1 = CDate(Left(dat1, 6) & Val(Right(dat1, 4) + 100))
dat2 = CDate(Left(dat2, 6) & Val(Right(dat2, 4) + 100))
If dat1 > dat2 Then dtemps = dat2: dat2 = dat1: dat1 = dtemps
A = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""y"")")
Maint = DateSerial(Year(Date) + 100, Month(Date), Day(Date))
anniv = CDate(Left(dat1, 6) & Year(Date) + 100)
If anniv < Maint Then dtemps = anniv: anniv = Maint: Maint = dtemps
M = Evaluate("=DATEDIF(" & CLng(Maint) & "," & CLng(anniv) & ",""ym"")")
J = Evaluate("=DATEDIF(" & CLng(Maint) & "," & CLng(anniv) & ",""md"")")
If dat1 < CDate(Left(dat2, 6) & Year(dat1)) Then M = 12 - M
'Debug.Print dat1 & vbCrLf & dat2 & vbCrLf & Maint & vbCrLf & anniv & vbCrLf & A & vbCrLf & M & " mois " & J & " jours"
Age_Anniversaire_dans = Array(" ", A, M, J)(A_ou_M_ou_J)
End Function