Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default DateDiff() problem

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default DateDiff() problem

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" wrote in message
...
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default DateDiff() problem

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" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default DateDiff() problem

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default DateDiff() problem

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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default DateDiff() problem

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

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default DateDiff() problem

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
datediff help ina Excel Programming 4 April 24th 06 09:44 AM
DateDIFF Paul Breslin Excel Programming 1 December 12th 05 03:09 PM
Help with datediff vba John Excel Programming 4 February 16th 05 01:56 PM
DateDiff("w") -- problem zSplash Excel Programming 9 January 4th 04 10:56 PM
DateDiff problem Antje Crawford Excel Programming 3 July 8th 03 09:44 PM


All times are GMT +1. The time now is 01:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"