Results 1 to 3 of 3

Thread: [RESOLVED] Excel - Calculate date range

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2012
    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
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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 Siddharth Rout; 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


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2012
    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
  •  



Click Here to Expand Forum to Full Width