Excel calculates age accurate to a few years of age a few months

Source: Internet
Author: User

First, the following conditions are met

1 years old or older

month of January or more,

Less than a month to write days.

Second, the macro code

Function Getdatediff (STARTD, ENDD)

Dim y%, m%, d%

If STARTD > Endd or Not IsDate (STARTD) or not IsDate (endd) Then Getdatediff = "Data Error"

y = DateDiff ("yyyy", STARTD, ENDD)

If DateSerial (ENDD), Month (STARTD), Day (STARTD)) > Endd Then

y = y-1

If y >= 1 Then GoTo 100

m = 12-month (STARTD) + Month (ENDD)


m = Month (endd)-Month (STARTD)

End If

If Day (ENDD) >= Day (STARTD) Or Day (ENDD) = Day (DateSerial (year (ENDD), Month (ENDD) + 1, 0)) Then

If Day (ENDD) >= Day (STARTD) Then d = Day (ENDD)-Day (STARTD)

If Day (ENDD) < Day (STARTD) and day (ENDD) = Day (DateSerial (ENDD), Month (ENDD) + 1, 0) Then d = Day (DateSerial (Ye AR (STARTD), Month (STARTD) + 1, 0))-Day (STARTD)


m = m-1

D = Day (DateSerial (STARTD), Month (STARTD) + 1, 0))-day (STARTD) + Day (ENDD)

End If

If m >= 1 Then d = 0

100:getdatediff = IIf (Y > 0, Y & "old", IIf (M > 0, M & "month", D & "Day"))

End Function

Sub get Month Day ()

Dim arr1, ARR2 ()

arr = Sheet1.range ("A2:b" & Sheet1.range ("A65536"). End (Xlup). Row)

ReDim arr2 (1 to UBound (arr), 1 to 1)

For i = 1 to UBound (arr)

ARR2 (i, 1) = Getdatediff (arr (i, 1), arr (I, 2))

Next I

Sheet1.range ("C2:c" & Sheet1.range ("A65536"). End (Xlup). Row) = arr2

End Sub

The code above is a macro code that is used in a module.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.