Calculating Days Between Dates

G

gti_jobert

Hi all,

I have two dates (30/03/2006 and 05/04/2006, for example)

How do I in VBA calculate the number of days between these two dates
BUT excluding the weekends?

Any Ideas Appreciated
TIA
 
G

gti_jobert

No matter guys, have came up with solution now;


Code:
--------------------

i = 0: V = 0
Do
dte = sDate + i
If (DatePart("w", dte, vbMonday) > 5) Then
'do nothing
Else
V = V + 1
End If
i = i + 1
Loop Until Left(dte, 8) = Left(eDate, 8)
 
B

bplumhoff

Hello,

I suggest not to take the analysis toolpak and not to count but to
calculate straightforward:

Option Explicit

Function daydiff(dtStart As Date, dtEnd As Date) As Long

daydiff = (dtEnd - Weekday(dtEnd, 2) + _
Weekday(dtStart, 2) - dtStart) _
/ 7 * 5 - min(5, Weekday(dtStart, 2)) _
+ min(5, Weekday(dtEnd, 2))

End Function

Function min(a As Long, b As Long) As Long
If a < b Then
min = a
Else
min = b
End If
End Function

Please have a look at http://www.sulprobil.com/html/date_formulas.html
and notice:
This formula is different from Excel's built-in function (analysis
toolpak). It counts from Startdate 24:00 until Enddate 24:00

HTH,
Bernd
 
B

Bob Phillips

Tom's answer is better.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
B

Bob Phillips

But even if you don't want to use the ATP, looping through all the dates is
overkill.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

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