PC Review


Reply
Thread Tools Rate Thread

DateDiff() problem

 
 
Brad
Guest
Posts: n/a
 
      26th Mar 2009
Excel 2007 (with compatibility to 2003

Sub CalcAge()
Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
Range("c5").Value)
End Sub

C7 = 8/4/1962
C5 = 2/4/2009

Expect to get 46, getting 47 - Is the problem with using Range().value?
--
Wag more, bark less
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      26th Mar 2009
It looks like DateDiff is simply doing this...

Year(EndDate) - Year(StartDate

You can see that by first trying these dates...

EndDate = #1/31/2009#
StartDate = #12/1/1962#

and then these...

EndDate = #12/31/2009#
StartDate = #1/1/1962#

How did you want to calculate the difference... round down to the nearest
full year or round up or down depending on if the excess is a more or less
than a half year?

--
Rick (MVP - Excel)


"Brad" <(E-Mail Removed)> wrote in message
news1A9A243-DEAD-4086-B2C7-(E-Mail Removed)...
> Excel 2007 (with compatibility to 2003
>
> Sub CalcAge()
> Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> Range("c5").Value)
> End Sub
>
> C7 = 8/4/1962
> C5 = 2/4/2009
>
> Expect to get 46, getting 47 - Is the problem with using Range().value?
> --
> Wag more, bark less


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      26th Mar 2009
Brad,

datediff can be a pain as a worksheet function or in VB so in Vb I would
generally use this

Range("IssAgeP").Value = Int(CStr((Range("c5").Value - Range("c7").Value) /
365.25))


But if you want to persist with datediff then this seems to work

Range("IssAgeP").Value = Int(DateDiff("d", Range("C7").Value,
Range("c5").Value) / 365.25)

Mike



"Brad" wrote:

> Excel 2007 (with compatibility to 2003
>
> Sub CalcAge()
> Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> Range("c5").Value)
> End Sub
>
> C7 = 8/4/1962
> C5 = 2/4/2009
>
> Expect to get 46, getting 47 - Is the problem with using Range().value?
> --
> Wag more, bark less

 
Reply With Quote
 
ryguy7272
Guest
Posts: n/a
 
      26th Mar 2009
This should do it for you!

Sub CalcAge()
Range("IssAgeP") = "The months between " & Range("C5") & " and " &
Range("C7") & " is " & DateDiff("m", Range("C7"), Range("C5")) & " months!"
'Assume IssAgeP is a NamedRnage for Cell E5
Range("E6") = (DateDiff("m", Range("C7"), Range("C5")) / 12) & " years!"
End Sub


HTH,
Ryan---
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Mike H" wrote:

> Brad,
>
> datediff can be a pain as a worksheet function or in VB so in Vb I would
> generally use this
>
> Range("IssAgeP").Value = Int(CStr((Range("c5").Value - Range("c7").Value) /
> 365.25))
>
>
> But if you want to persist with datediff then this seems to work
>
> Range("IssAgeP").Value = Int(DateDiff("d", Range("C7").Value,
> Range("c5").Value) / 365.25)
>
> Mike
>
>
>
> "Brad" wrote:
>
> > Excel 2007 (with compatibility to 2003
> >
> > Sub CalcAge()
> > Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> > Range("c5").Value)
> > End Sub
> >
> > C7 = 8/4/1962
> > C5 = 2/4/2009
> >
> > Expect to get 46, getting 47 - Is the problem with using Range().value?
> > --
> > Wag more, bark less

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      26th Mar 2009
forget the cstr

Range("h1").Value = Int(((Range("c5").Value - Range("c7").Value) / 365.25))



"Mike H" wrote:

> Brad,
>
> datediff can be a pain as a worksheet function or in VB so in Vb I would
> generally use this
>
> Range("IssAgeP").Value = Int(CStr((Range("c5").Value - Range("c7").Value) /
> 365.25))
>
>
> But if you want to persist with datediff then this seems to work
>
> Range("IssAgeP").Value = Int(DateDiff("d", Range("C7").Value,
> Range("c5").Value) / 365.25)
>
> Mike
>
>
>
> "Brad" wrote:
>
> > Excel 2007 (with compatibility to 2003
> >
> > Sub CalcAge()
> > Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> > Range("c5").Value)
> > End Sub
> >
> > C7 = 8/4/1962
> > C5 = 2/4/2009
> >
> > Expect to get 46, getting 47 - Is the problem with using Range().value?
> > --
> > Wag more, bark less

 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      30th Mar 2009
I was wanted it do work in a similar fashion as the function datedif(), I
modified the code so that it works - Thanks for the input
--
Wag more, bark less


"Rick Rothstein" wrote:

> It looks like DateDiff is simply doing this...
>
> Year(EndDate) - Year(StartDate
>
> You can see that by first trying these dates...
>
> EndDate = #1/31/2009#
> StartDate = #12/1/1962#
>
> and then these...
>
> EndDate = #12/31/2009#
> StartDate = #1/1/1962#
>
> How did you want to calculate the difference... round down to the nearest
> full year or round up or down depending on if the excess is a more or less
> than a half year?
>
> --
> Rick (MVP - Excel)
>
>
> "Brad" <(E-Mail Removed)> wrote in message
> news1A9A243-DEAD-4086-B2C7-(E-Mail Removed)...
> > Excel 2007 (with compatibility to 2003
> >
> > Sub CalcAge()
> > Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> > Range("c5").Value)
> > End Sub
> >
> > C7 = 8/4/1962
> > C5 = 2/4/2009
> >
> > Expect to get 46, getting 47 - Is the problem with using Range().value?
> > --
> > Wag more, bark less

>
>

 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      30th Mar 2009
I agree that datediff could work better - I modified the code by looking at
the month and day as well as the year to get what was needed

Thanks!
--
Wag more, bark less


"Mike H" wrote:

> Brad,
>
> datediff can be a pain as a worksheet function or in VB so in Vb I would
> generally use this
>
> Range("IssAgeP").Value = Int(CStr((Range("c5").Value - Range("c7").Value) /
> 365.25))
>
>
> But if you want to persist with datediff then this seems to work
>
> Range("IssAgeP").Value = Int(DateDiff("d", Range("C7").Value,
> Range("c5").Value) / 365.25)
>
> Mike
>
>
>
> "Brad" wrote:
>
> > Excel 2007 (with compatibility to 2003
> >
> > Sub CalcAge()
> > Range("IssAgeP").Value = DateDiff("yyyy", Range("C7").Value,
> > Range("c5").Value)
> > End Sub
> >
> > C7 = 8/4/1962
> > C5 = 2/4/2009
> >
> > Expect to get 46, getting 47 - Is the problem with using Range().value?
> > --
> > Wag more, bark less

 
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
Re: DateDiff Problem Douglas J. Steele Microsoft Access Forms 0 22nd Jan 2010 05:37 PM
Re: DateDiff Problem John Spencer Microsoft Access Queries 0 12th Dec 2006 04:50 PM
Re: DateDiff Problem Allen Browne Microsoft Access Queries 0 12th Dec 2006 04:03 PM
DateDiff Problem barnabythebear Microsoft Access Queries 3 27th Nov 2003 11:23 AM
DateDiff problem Antje Crawford Microsoft Excel Programming 3 8th Jul 2003 09:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 AM.