Here is a function that calculates the number of working days between two
dates. It includes looking up dates in a Holiday table to determine if a
week day is a holiday. If you have a holiday table in your app, you will
need to change the name [Holdate] to whatever your's is. If you don't
have a
holiday table, I would suggest one. Mine has only two fields, Holdate
(date/time) that is a non working day and a description field to identifiy
the holiday. If you don't want to omit holidays, you can remove the code
that checks for holidays.
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer
'Calculates the number of working days between two dates
'dtmStart - the first day to include in the range
'dtmEnd - the last day to include in the range
'Returns the number of working days between the two dates
'Both dates are counted if they are working days
Dim intTotalDays As Integer ' Counter for number of days
Dim dtmToday As Date ' To increment the date to compare
intTotalDays = DateDiff("d", dtmStart, dtmEnd) + 1 'Start with total
days
'Add one to include
First Day
dtmToday = dtmStart 'Initiate compare
date
Do Until dtmToday > dtmEnd
If Weekday(dtmToday, vbMonday) > 5 Then 'It is Saturday or
Sunday
intTotalDays = intTotalDays - 1 'Take one day away
for Weekend day
ElseIf Not IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmToday & "#")) Then 'It is a holiday
intTotalDays = intTotalDays - 1 'Take one day away
for the Holiday
End If
dtmToday = DateAdd("d", 1, dtmToday) 'Add a day for next
compare
Loop 'Until dtmToday > dtmEnd All days have been
compared
CalcWorkDays = intTotalDays 'Return the value
End Function
Andy said:
Hi Everyone,
I have this database and there are two fields, Start Date and End Date. I
would like somehow to calculate how many working days are between End
Date
and Start Date and save the value into another field.
Can anyone help me with this please.
Regards,
Anguel