Results 1 to 3 of 3

Thread: [RESOLVED] Excel - Calculate date range

  1. #1
    Member
    Join Date
    Mar 12
    Posts
    33

    Resolved [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

  2. #2
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,415

    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
    Last edited by koolsid; Aug 26th, 2012 at 03:07 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    Member
    Join Date
    Mar 12
    Posts
    33

    Re: Excel - Calculate date range

    Hi.

    I am very grateful for the help!

    thank you very much!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •