Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Calculating Age In VBA (Chip Pearson)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick S. - 31 Jan 2008 20:15 GMT
I am not understanding something of Chips function.
If I run a macro with "MsgBox Age(12 / 3 / 61, 1 / 31 / 8)" I get "0 years 0
months 0 days" returned?  Obviously there is at least 46 years here.  What do
I have formatted wrong?

'======author Chip pearson
Function Age(Date1 As Date, Date2 As Date) As String
   Dim Y As Integer
   Dim M As Integer
   Dim D As Integer
   Dim Temp1 As Date
   Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
   Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
   M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
   D = Day(Date2) - Day(Date1)
   If D < 0 Then
       M = M - 1
       D = Day(DateSerial(Year(Date2), Month(Date2) + 1, 0)) + D + 1
   End If
   Age = Y & " years " & M & " months " & D & " days"
End Function
'======
Signature

Regards

VBA.Noob.Confused
XP Pro
Office 2007

Mike H - 31 Jan 2008 20:24 GMT
Maybe

=Age(E1,TODAY())

Where E1 is the DOB

Mike

> I am not understanding something of Chips function.
> If I run a macro with "MsgBox Age(12 / 3 / 61, 1 / 31 / 8)" I get "0 years 0
[quoted text clipped - 18 lines]
> End Function
> '======
Rick S. - 31 Jan 2008 20:25 GMT
ActiveUser.BrainCells.Add(12HundredBazillion)
Yea, thats better.

Works fine with variables.

'======
Dim x As Date
Dim y As Date
x = InputBox("Enter Date 1")
y = InputBox("Enter Date 2")
MsgBox Age(y, x)
'======
Signature

Regards

VBA.Noob.Confused
XP Pro
Office 2007

> I am not understanding something of Chips function.
> If I run a macro with "MsgBox Age(12 / 3 / 61, 1 / 31 / 8)" I get "0 years 0
[quoted text clipped - 18 lines]
> End Function
> '======
Alan Beban - 31 Jan 2008 20:38 GMT
> ActiveUser.BrainCells.Add(12HundredBazillion)

I think it's brazillion. From the greeting card on which Cheney is
reporting to Bush that they have a commitment for 100 Brazilian troops
to help in Iraq, and Bush responds "That's terrific!" And you open the
card to Bush asking, "By the way, how many is 100 brazillion?"

Alan Beban
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.