PC Review


Reply
Thread Tools Rate Thread

Calculating Age In VBA (Chip Pearson)

 
 
Rick S.
Guest
Posts: n/a
 
      31st Jan 2008
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
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      31st Jan 2008
Maybe

=Age(E1,TODAY())

Where E1 is the DOB

Mike

"Rick S." wrote:

> 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
> '======
> --
> Regards
>
> VBA.Noob.Confused
> XP Pro
> Office 2007
>

 
Reply With Quote
 
Rick S.
Guest
Posts: n/a
 
      31st Jan 2008
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)
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007



"Rick S." wrote:

> 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
> '======
> --
> Regards
>
> VBA.Noob.Confused
> XP Pro
> Office 2007
>

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      31st Jan 2008
Rick S. wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thank you Chip Pearson =?Utf-8?B?TWljaGFlbA==?= Microsoft Excel Misc 3 5th Feb 2005 08:35 PM
Thanks, Chip Pearson Susan Ramlet Microsoft Excel Misc 1 27th Jul 2004 08:38 PM
Chip Pearson Ricardo Microsoft Excel Programming 0 10th Nov 2003 07:51 PM
CHIP PEARSON - THANX bertieBassett Microsoft Excel Programming 0 3rd Nov 2003 02:01 PM
Re: Chip Pearson or someone Chip Pearson Microsoft Excel Programming 3 18th Sep 2003 05:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:45 PM.