number of days between dates

J

JT

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
 
C

Chip Pearson

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
 
M

merjet

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
 
M

Myrna Larson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top