Let’s say you have your Date of Birth entered in Cell A2.

You want the Age in Years to appear in Cell B2.

Age from DOB

Age from DOB

Code

‘Finding Age of Patient is displaying in Label

Dim b As Date

b = Sheets(“Sheet1”).Range(“A2”).Value

Dim d As Integer

d1 = Day(Date)
d2 = Day(b)
m1 = Month(Date)
m2 = Month(b)
y1 = Year(Date)
y2 = Year(b)

If m1 > m2 Then
d = y1 – y2
ElseIf (m1 >= m2 And d1 >= d2) Then
d = y1 – y2
Else: d = y1 – y2 – 1
End If

Sheets(“Sheet1”).Range(“B2″).Value = d & ” years”

End Sub

 

Explanation

  • Dim b As Date
    b = Sheets(“Sheet1”).Range(“A2”).Value 

    You declare a variable ‘b’ as a Date.
    You pick up the value from Cell A2 and put it into b.

  • d1 = Day(Date)
    d2 = Day(b)
    m1 = Month(Date)
    m2 = Month(b)
    y1 = Year(Date)
    y2 = Year(b)

    d1 = fetch the Day part from Today’s Date
    d2 = fetch the Day part from b, that is the Date of Birth
    m1 = fetch the Month part from Today’s Date
    m2 = fetch the Month part from b, that is the Date of Birth
    y1 = fetch the Year part from Today’s Date
    y2 = fetch the Year part from b, that is the Date of Birth

  • If m1 > m2 Then
               d = y1 – y2
    ElseIf (m1 >= m2 And d1 >= d2) Then
               d = y1 – y2
    Else: d = y1 – y2 – 1
    End If 

    Logic to calculate no. of years and the value is put in a variable d, which is earlier declared as an integer

  • Sheets(“Sheet1”).Range(“B2″).Value = d & ” years”
    Paste the value of d to cell B2.