Mike-31
7439Messages postés
17 février 2008Date d'inscription
31 mai 2012Dernière intervention
25 juin 2011 à 11:57
Salut le fil,
Il existe une fonction personnalisé editée par Microsoft qui décompte les années à coller dans un module. si besoin faire signe
Public Function AgeFunc(stdate As Variant, endate As Variant)
Dim stvar As String
Dim stmon As String
Dim stday As String
Dim styr As String
Dim endvar As String
Dim endmon As String
Dim endday As String
Dim endyr As String
Dim stmonf As Integer
Dim stdayf As Integer
Dim styrf As Integer
Dim endmonf As Integer
Dim enddayf As Integer
Dim endyrf As Integer
Dim years As Integer
Dim fx As Integer
fx = 0
stvar = sfunc("/", stdate)
stmon = Left(stdate, sfunc("/", stdate) - 1)
stday = Mid(stdate, stvar + 1, sfunc("/", stdate, sfunc("/", stdate) + 1) - stvar - 1)
If Len(stday) = 1 Then fx = fx + 1
If Len(stmon) = 2 Then fx = fx + 1
styr = Right(stdate, Len(stdate) - (sfunc("/", stdate) + 1) - stvar + fx)
stmonf = CInt(stmon)
stdayf = CInt(stday)
styrf = CInt(styr)
If stmonf < 1 Or stmonf > 12 Or stdayf < 1 Or stdayf > 31 Or styrf < 1 Then
AgeFunc = "Invalid Date"
Exit Function
End If
fx = 0
endvar = sfunc("/", endate)
endmon = Left(endate, sfunc("/", endate) - 1)
endday = Mid(endate, endvar + 1, sfunc("/", endate, sfunc("/", endate) + 1) - endvar - 1)
If Len(endday) = 1 Then fx = fx + 1
If Len(endmon) = 2 Then fx = fx + 1
endyr = Right(endate, Len(endate) - (sfunc("/", endate) + 1) - endvar + fx)
endmonf = CInt(endmon)
enddayf = CInt(endday)
endyrf = CInt(endyr)
If endmonf < 1 Or endmonf > 12 Or enddayf < 1 Or enddayf > 31 Or endyrf < 1 Then
AgeFunc = "Invalid Date"
Exit Function
End If
years = endyrf - styrf
If stmonf > endmonf Then
years = years - 1
End If
If stmonf = endmonf And stdayf > enddayf Then
years = years - 1
End If
If years < 0 Then
AgeFunc = "Invalid Date"
Else
AgeFunc = years
End If
End Function
Public Function sfunc(x As Variant, y As Variant, Optional z As Variant)
sfunc = Application.WorksheetFunction.Search(x, y, z)
End Function
-
A+
Mike-31
Une période d'échec est un moment rêvé pour semer les graines du savoir.
En tout cas grand merci à néné, car j'avais une formule beaucoup plus (et inutilement) compliquée ...