+ Reply to Thread
Results 1 to 3 of 3

Difficulty with an Age macro

Hybrid View

  1. #1
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Difficulty with an Age macro

    I'm working on project where I need a person's age on a specified date displayed as a decimal value. The code I have so far displays the age as a whole number.

    Sub CompileAgeOnSpecificDate()
    Dim ldr As Long
    Dim cInput As Long
    Dim dCol As Long
    Dim x As Date
    Dim MyRange As Range
        On Error Resume Next
            Application.DisplayAlerts = False
                cInput = Application.InputBox _
                 (Prompt:="Enter the column number containing the birth date", _
                        Title:="Calculate the age", Type:=1)
                x = Application.InputBox _
                 (Prompt:="Enter the date on which to calculate age" & vbCr & vbCr & _
                 "Example:   Feb 08 1955 or mm/dd/yyyy", _
                        Title:="Date", Type:=2)
                dCol = Application.InputBox _
                 (Prompt:="Enter the column number to place the age", _
                        Title:="Location", Type:=1)
        On Error GoTo 0
        Application.DisplayAlerts = True
            If cInput = 0 Then
                Exit Sub
            Else
            ldr = Cells(Rows.Count, cInput).End(xlUp).Row
            If ldr > 2 Then
            Set MyRange = Range(Cells(2, cInput), Cells(ldr, cInput))
            Dim c
            For Each c In MyRange
            If c.Value <> 0 Then
            iYears = DateDiff("yyyy", c.Value, x, Date)
    'Require the above age result to be a decimal value....
            c.Offset(0, dCol - cInput) = iYears
            End If
            Next c
            MsgBox "Age for column " & cInput & " was placed in column " & dCol & " based on the date of " & x & " ...    "
            End If
    End If
    End Sub
    I've attached a sample workbook.

    Mister P
    Attached Files Attached Files
    Last edited by Mister P; 01-05-2011 at 07:00 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Difficulty with an Age macro

    Hello Mister P,

    The problem is you are returning the difference in whole years. There are many decimal fractions that could be used like days or months. I decided on months. Here is the loop with the change to reflect the age in years with the decimal based on months.
                    For Each c In MyRange
                      If c.Value <> 0 Then
                         iYears = DateDiff("m", c.Value, x, Date) / 12
                        'Require the above age result to be a decimal value....
                         c.Offset(0, dCol - cInput) = iYears
                      End If
                    Next c
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Difficulty with an Age macro

    Leith,
    This is exactly what I was trying to accomplish. Perfect for my application. Thank you so much.

    Mister P

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1