[RESOLVED] Excel - Calculate date range
Hi.
I need to calculate date range. Until then beauty. More when I run the program exits not the exact calculation:
Example: Date of birth: 26/11/1988
Current Date: 26/08/2012
Correct Score: 23
Program Result: 24
What I realized is that getting the range of years and not the entire date for calculation.
Would pick up the entire range "dd / mm / yyyy"?
Code:
Sub Teste()
Dim Nascimento As Date, AnoAtual As Date, Idade As Double, Idade2005 As Date, Idadeem2005 As Double
Nascimento = InputBox("Enter the date of birth:")
AnoAtual = InputBox("Enter the Current Year:")
Idade = DateDiff("yyyy", Nascimento, AnoAtual)
Idade2005 = "31 / 12 / 2005"
Idadeem2005 = DateDiff("yyyy", Nascimento, Idade2005)
MsgBox "Essa pessoa tem: " & Idade & Chr(13) & _
"His age in 2005 will be: " & Idadeem2005
End Sub
Re: Excel - Calculate date range
I would recommend having a look at this msdn link.
Try this code. This will give you 23.
Code:
Sub Sample()
Debug.Print dhAge(CDate("26/11/1988"), CDate("26/08/2012"))
End Sub
Function dhAge(dtmBD As Date, Optional dtmDate As Date = 0) As Integer
' Calculate a person's age, given the person's birth date and
' an optional "current" date.
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
dtmDate = Date
End If
dhAge = DateDiff("yyyy", dtmBD, dtmDate) + _
(dtmDate < DateSerial(Year(dtmDate), Month(dtmBD), _
Day(dtmBD)))
End Function
Re: Excel - Calculate date range
Hi.
I am very grateful for the help!
thank you very much!!