Re : Quel age a -t-il ?
voici un exemple en vba
{code}
Sub main()
Dim todaydate As Date
Dim howold As Date
todaydate = Format(Now, "DD/MM/YYYY")
howold = InputBox("Entrer votre date de naissance sous ce format DD/MM/YYYY")
'On Error GoTo errorhandler
'errorhandler:
'MsgBox "Vous devez re-entrer votre date de naissance sous ce format dd/mm/yyyy"
Call YMD(howold, todaydate)
End Sub
Function YMD(howold As Date, todaydate As Date) As String
Dim years, months, days, m
years = Year(todaydate) - Year(howold)
If Month(howold) > Month(todaydate) Then
years = years - 1
End If
If Month(todaydate) < Month(howold) Then
months = 12 - Month(howold) + Month(todaydate)
Else
months = Month(todaydate) - Month(howold)
End If
If Day(todaydate) < Day(howold) Then
months = months - 1
If Month(todaydate) = Month(howold) Then
years = years - 1
months = 11
End If
End If
days = Day(todaydate) - Day(howold)
If days < 0 Then
m = CInt(Month(todaydate)) - 1
If m = 0 Then m = 12
Select Case m
Case 1, 3, 5, 7, 8, 10, 12
days = 31 + days
Case 4, 6, 9, 11
days = 30 + days
Case 2
If (Year(todaydate) Mod 4 = 0 And Year(todaydate) _
Mod 100 <> 0) Or Year(todaydate) Mod 400 = 0 Then
days = 29 + days
Else
days = 28 + days
End If
End Select
End If
YMD = "Vous etes agé de " + CStr(years) + " ans " + CStr(months) _
+ " mois " + CStr(days) + " jours " '+ " old "
Range("D7:I9").Select
With Selection.Font
.Name = "Arial Narrow"
.FontStyle = "Bold"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Interior
.ColorIndex = 8
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Range("D8:I8").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Selection.Merge
ActiveCell = YMD
Range("a1").Select
' ActiveCell.Font.ColorIndex = vbBlue
End Function
{\code}