Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Business Days Calculations that accounts for holidays 1

Status
Not open for further replies.

woro2006

IS-IT--Management
Dec 24, 2006
18
US
I found somewhere in the forum that I can calculate business days between two days. Suppose I have a table that contains all the holiday day dates (table = tblholiday, field name = hoilday). How can I extend the following function to allow me to account for the holidays in tblholiday. Please write the complete code (if possible), as I am a newbie. Thanks!

Function BusinessDays(dtStartDate As Date, dtEndDate As Date)
'Returns number of business days between two dates as an integer
'Returns -1 if the start date is greater than the end date

Dim intDayCount As Integer
Dim J As Integer
Dim dtNextDate As Date

'----------------------------------------------------------------
'- Set the first date to check -
'----------------------------------------------------------------
dtNextDate = dtStartDate

'----------------------------------------------------------------
'- Return error code of -1 if Start Date is later than End Date -
'----------------------------------------------------------------
If dtStartDate > dtEndDate Then
BusinessDays = -1
Else

'----------------------------------------------------------------
'- Loop through all dates between Start Date and End Date -
'----------------------------------------------------------------
Do Until dtNextDate >= dtEndDate

'----------------------------------------------------------------
'If it's a weekday (Monday to Friday) add 1 to the day counter
'----------------------------------------------------------------
If WeekDay(dtNextDate, vbMonday) <= 5 Then
intDayCount = intDayCount + 1
End If

dtNextDate = dtNextDate + 1
Loop


'------------------------------------------------------------------
'- Return number of business days between the start and end dates -
'------------------------------------------------------------------
BusinessDays = intDayCount

End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top