Jez said:
Hi,
Please help.... I am trying to workout the number of Working Days between
the Visit Date and a Close Job Date. How can I do this, I know in excel its
NetworkDays(A1,B1) how is this possible in Access?
Also once I have worked out the number of days between these dates how can I
group all the number of days, for example... All days between 0&6 is in week,
all days between 7&13 is week2 and days between 14&20 is week3?
Thanks,
Jez
hi jez,
try those functions
Function DateSpan(BegDate As Date, EndDate As Date) As Variant
'FROM
http://support.microsoft.com/kb/213182/en-us
'return an Array with all the days between BegDate and EndDate
Dim DateArray() As Variant, i As Integer, Span As Integer
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
Span = EndDate - BegDate + 1
ReDim DateArray(1 To Span)
For i = 1 To Span
DateArray(i) = BegDate + i - 1
Next
DateSpan = DateArray
End Function
'Return the number of working days between BeginDate and EndDate
'excluding sunday and saturday and the Dates included in Holidays Array
'it should work Like Excel Function NetworkDays
Function WorkingDays(BeginDate As Date, EndDate As Date, Optional Holidays
As Variant = Null) As Integer
Dim Days As Integer
Dim nonWorkingDays As Integer
Dim i As Integer
Dim dayOfWeek As Integer
If EndDate < BeginDate Then 'case endDate < Begindate not considered
WorkingDays = -1
Exit Function
End If
Days = DateDiff("d", BeginDate, EndDate) + 1
nonWorkingDays = 0
For i = 0 To Days - 1
dayOfWeek = Weekday((BeginDate + i), vbSunday)
If dayOfWeek = vbSaturday Or dayOfWeek = vbSunday Then
nonWorkingDays = nonWorkingDays + 1
End If
Next i
If IsNull(Holidays) Then
WorkingDays = Days - nonWorkingDays
Else
Dim curDay As Date
For i = 1 To UBound(Holidays)
curDay = Holidays(i)
dayOfWeek = Weekday(curDay, vbSunday)
If curDay >= BeginDate And curDay <= EndDate And (dayOfWeek <>
vbSaturday And dayOfWeek <> vbSunday) Then
nonWorkingDays = nonWorkingDays + 1
End If
Next i
WorkingDays = Days - nonWorkingDays
End If
End Function
Ho to use:
Dim myHolidays(1 to 3) as date
myHolidays(1) =Cdate("2006/12/31")
myHolidays(2) =Cdate("2007/1/31")
myHolidays(3) =Cdate("2007/04/25")
Debug.print WorkingDays(Cdate("2006/12/30"), Cdate("2007/06/30"),
myHolidays)
or using DateSpan
Debug.print WorkingDays(Cdate("2006/12/30"), Cdate("2007/06/30"),
DateSpan(Cdate("2007/02/20"), Cdate("2007/03/05")))
Try it and give me a feedback, if you can.
To group by week see the function
DatePart(interval, date[,firstdayofweek[, firstweekofyear]])
Bye