Function DATEDIFFAMJ4$(ByVal dat1 As Date, ByVal dat2 As Date, Optional JustYear As Boolean = False)
'**************************************
'fonction DateDiffAMJ V°4
'auteur:patricktoulon sur Exceldownloads
'date de mise en jour V°4 : 04/07/2021
' mise a jour supplémentaire
' ajout de l'argument boolean "JustYear" en optional pour ne récupérer que les années
'*************************************
Dim A$, M$, J$, Dtemp$, et$, yeardécalée&, y
If dat1 > dat2 Then Dtemp = dat2: dat2 = dat1: dat1 = Dtemp
If Year(dat1) < 1904 Then If Year(dat1) Mod 4 <> 0 Or Year(dat1) Mod 400 <> 0 Then y = 2020 Else y = 1904
If Year(dat1) < y Then
'on decale la date la plus ancienne (Dat1)à l'année 1904
yeardécalée = Abs((Year(dat1) - y))
dat1 = DateSerial(Year(dat1) + yeardécalée, Month(dat1), Day(dat1))
dat2 = DateSerial(Year(dat2) + yeardécalée, Month(dat2), Day(dat2))
End If
A = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""y"")")
M = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""ym"")")
J = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""md"")")
A = IIf(A = 0, "", IIf(A = 1, A & " an", A & " ans"))
M = IIf(M = 0, "", IIf(M = 1, M & " mois", M & " mois"))
J = IIf(J = 0, "", IIf(J = 1, "1 jour", J & " jours"))
et = IIf(Val(A) > 0 Or Val(M) > 0, IIf(Val(J) > 0, " et ", " "), "")
DATEDIFFAMJ4 = Application.Trim(A & IIf(Not JustYear, " " & M & " " & et & J, ""))
End Function
Tu peux expliquer pourquoi 1904 et 2020 ? Je ne vois pas trop la raison de devoir utiliser une 2nde année.
eric
Sub test2()
MsgBox CDate("03/03/20219")
End Sub
Function DATEDIFF_AMJ4$(ByVal dat1 As Date, Optional ByVal dat2 As Date = 0, Optional JustYear As Boolean = False)
'**************************************
'fonction DateDiffAMJ V°4
'auteur:patricktoulon sur Exceldownloads
'date de mise en jour V°4:04/07/2021
' mise a jour supplementaire
' ajout de l'argument boolean "JustYear" pour ne récuprérer que les années
'*************************************
Dim A$, M$, J$, Dtemp$, et$, yeardécalée&, y
If dat2 = 0 Then dat2 = Date
If dat1 > dat2 Then Dtemp = dat2: dat2 = dat1: dat1 = Dtemp
If Year(dat2) > 9999 Or Year(dat1) < 1700 Then DATEDIFF_AMJ4$ = "yearLimite!!": Exit Function
'If Year(dat1) < 1900 Then If Year(dat1) Mod 400 = 0 Then y = 2020 Else y = 1905
'If Year(dat1) < y Then
If Year(dat1) < 1900 Then
if dat1< cdate("09/12/1760") then dat1=dat1-11
'on decale la date la plus ancienne (Dat1)à l'année +2000
yeardécalée = 2000 'Abs((Year(dat1) - y))
dat1 = DateSerial(Year(dat1) + yeardécalée, Month(dat1), Day(dat1))
dat2 = DateSerial(Year(dat2) + yeardécalée, Month(dat2), Day(dat2))
End If
A = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""y"")")
M = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""ym"")")
J = Evaluate("=DATEDIF(" & CLng(dat1) & "," & CLng(dat2) & ",""md"")")
A = IIf(A = 0, "", IIf(A = 1, A & " an", A & " ans"))
M = IIf(M = 0, "", IIf(M >= 1, IIf(Val(J) = 0, " et ", "") & M & " mois", M & " mois"))
J = IIf(J = 0, "", IIf(J = 1, "1 jour", J & " jours"))
et = IIf(Val(A) > 0 Or Val(M) > 0, IIf(Val(J) > 0, " et ", " "), "")
DATEDIFF_AMJ4 = Application.Trim(A & IIf(Not JustYear, " " & M & " " & et & J, ""))
End Function
C'est normal que cette partie de ton code soit du texte ?VB:'If Year(dat1) < 1900 Then 'If Year(dat1) Mod 400 = 0 Then y = 2020 Else y = 1905 'If Year(dat1) < y Then