Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 96
Default number of days between dates

I need to calculate the number of days between dates but
am not having much luck with the datediff function.
Here's the code I'm trying to use:

Dim vNumberofDays As Integer
Dim vDate1 As Date
Dim vDate2 As Date

Range("A1").Select
ActiveCell.Offset(1, 25).Select

Do Until IsEmpty(ActiveCell.Offset(0, -3)) = True
vDate1 = ActiveCell.Offset(0, -16)
vDate2 = ActiveCell.Offset(0, -9)
vNumberofDays = DateDiff(d, vDate1, vDate2)
ActiveCell = vNumberofDays
vNumberofDays = 0
ActiveCell.Offset(1, 0).Select
Loop

Any help would be appreciated.....Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default number of days between dates

You can just subtract the earlier date from the later date to get
the number of days. E.g.,
vNumberOfDays = vDate2-vDate1


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"JT" wrote in message
...
I need to calculate the number of days between dates but
am not having much luck with the datediff function.
Here's the code I'm trying to use:

Dim vNumberofDays As Integer
Dim vDate1 As Date
Dim vDate2 As Date

Range("A1").Select
ActiveCell.Offset(1, 25).Select

Do Until IsEmpty(ActiveCell.Offset(0, -3)) = True
vDate1 = ActiveCell.Offset(0, -16)
vDate2 = ActiveCell.Offset(0, -9)
vNumberofDays = DateDiff(d, vDate1, vDate2)
ActiveCell = vNumberofDays
vNumberofDays = 0
ActiveCell.Offset(1, 0).Select
Loop

Any help would be appreciated.....Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default number of days between dates

I need to calculate the number of days between dates but
am not having much luck with the datediff function.
[snip]
vNumberofDays = DateDiff(d, vDate1, vDate2)


The first argument needs to be in quotes, i.e. "d".

HTH,
Merjet



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default number of days between dates

As Chip mentioned, to get a difference in days, you can just subtract, but the
problem with your code is that the first argument to DateDiff must be a
string.

You used the *variable* named d. This would be interpreted by VBA as a variant
variable, and since you didn't set its value, it is an empty string. You
should probably be writing something like

vNumberofDays = DateDiff("d", vDate1, vDate2)

You can eliminate these sorts of errors if you include the line

Option Explicit

at the top of the module. If you do this, the code will not compile; you'll
get an error that d has not been dim'd.


On Thu, 5 Aug 2004 12:58:20 -0700, "JT"
wrote:

I need to calculate the number of days between dates but
am not having much luck with the datediff function.
Here's the code I'm trying to use:

Dim vNumberofDays As Integer
Dim vDate1 As Date
Dim vDate2 As Date

Range("A1").Select
ActiveCell.Offset(1, 25).Select

Do Until IsEmpty(ActiveCell.Offset(0, -3)) = True
vDate1 = ActiveCell.Offset(0, -16)
vDate2 = ActiveCell.Offset(0, -9)
vNumberofDays = DateDiff(d, vDate1, vDate2)
ActiveCell = vNumberofDays
vNumberofDays = 0
ActiveCell.Offset(1, 0).Select
Loop

Any help would be appreciated.....Thanks


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
Need to calculate the number of days between two dates on a 30 day James Dasher Excel Worksheet Functions 5 April 11th 09 02:46 AM
Number of Weekend Days between 2 Dates Q Seanie Excel Worksheet Functions 1 November 5th 08 04:40 PM
How to subtract 2 dates to get number of days.....please Nick Excel Worksheet Functions 3 November 10th 06 01:03 PM
Calculating number of days between two dates that fall between two other dates [email protected] Excel Discussion (Misc queries) 5 October 26th 05 06:18 PM
convert dates to number of days milk0s Excel Worksheet Functions 2 September 28th 05 01:31 PM


All times are GMT +1. The time now is 03:13 AM.

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

About Us

"It's about Microsoft Excel"