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
Bookmarks