Field code to calculate number of years

A

Austin

I need to insert a field code that can take the current date and then
subtract a certain year and display the result. For instance I have a person
who started working in 1970 and today is May 10 2005 so he has 35 years of
service.

If I could also calculate the exact start day mm/dd/yyyy and have the
current date subtract, that may be helpful but not necessary - I don't need
to break it down to months.

Thanks!
 
D

Doug Robbins

If you want a field, do a google search for macropod.

How about a macro:

' a Macro to calculate the age in years, months and days

' based on a birthdate entered in cell A2 and insert the

' age in cell A3

' Macro created 16/9/00 by Doug Robbins

'

Dim Years As Long

Dim Months As Long

Dim Days As Long

Dim Birthrange As Range

Dim Birthday As Long

Dim Day As Long

Dim Age As String

Dim Agedays As String

Dim Yearnow As Long

Dim Monthnow As Long

Monthnow = Val(Format(Date, "m"))

Yearnow = Val(Format(Date, "yyyy"))

Dim mon$(12)

mon$(1) = "January": mon$(2) = "February": mon$(3) = "March": mon$(4) =
"April": mon$(5) = "May": mon$(6) = "June": mon$(7) = "July": mon$(8) =
"August": mon$(9) = "September": mon$(10) = "October": mon$(11) =
"November": mon$(12) = "December"

Dim monthdays$(12)

If ((Yearnow Mod 4 = 0 And Calyear Mod 400 = 0) Or (Calyear Mod 4 = 0
And Calyear Mod 100 <> 0)) Then

monthdays$(1) = "31": monthdays$(2) = "29": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"

Else

monthdays$(1) = "31": monthdays$(2) = "28": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"

End If

Set Birthrange = ActiveDocument.Tables(1).Cell(1, 2).Range

Birthrange.End = Birthrange.End - 1

Birthdate = Birthrange

Years = DateDiff("yyyy", Birthdate, Date)

Months = DateDiff("m", Birthdate, Date) - Years * 12

Birthday = Format(Birthdate, "d")

Day = Format(Date, "d")

Days = Day - Birthday

If Days > 0 Then

Agedays = " and " & Format(Days) & " days."

ElseIf Days < 0 Then

Agedays = " and " & Format(monthdays$(Monthnow) - Birthday + Day) & "
days."

Months = Months - 1

Else

Agedays = "."

End If

Age = Format(Years) & " years, " & Format(Months) & " Months" & Agedays

ActiveDocument.Tables(1).Cell(1, 3).Range.Text = Age

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

Charles Kenyon

This is more complex than you might imagine, but it can be done. See
http://addbalance.com/word/datefields2.htm for information on the different
kinds of ways to make a date calculation work. It includes links to pages
with fields and an explanation of different macros that can be used instead
of fields. Also, note that if 24 yrs, 2 mos. is not the same as 25 yrs,
simply subtracting the years portion of a date will not give you a valid
result; you'll need to figure to at least months, and probably days, and
then truncate or round.

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top