Good day to all.
(Posted yesterday in the wrong forum, put a note to refer here).
I have studied thread181-51747: Is calculating BUSINESS HOURS possible...?, thread705-1207464: Calculate business hours between two times, FAQ181-261: Calculate working days between two dates and anything related to the calculation of business hours, especially by Michael Red. Unfortunately, VBA is unknown to me and I am not a programmer. I got confused as to what code version was the appropriate one for our needs.
My company requires a way to calculate working hours and minutes between dates while removing weekends and holidays (coffee breaks and lunch calculation are not required). The results are to be stored in a separate table.
Assuming that:
Table:
"tblHolidays" contains [holidate] and [holiname] fields where [holiname] is "text" and [holidate] is "GeneralDate" to capture the HH:MM
"tblDates" contains [stdt] (StartDate) and [enddt] (End Date) where both fields are "GeneralDate"
The code below is saved in a module named "WH" (for working hours)
When I activate this code, it required a macro name, which I created to run this code: basDlyHrs («StDt», «EndDt») however an error occurs (cant find "stdt"
My questions are as follow:
Will the code below provide both business hours and min?
If yes, what am I doing wrong?
I do am sorry in advance if this is not clear enough however I think I did my best after a full day in trying to make this work. I envy your knowledge..
Thanks in advance
Public Function basDlyHrs(StDt As Date, EndDt As Date) As Double
'Michhael Red 1/1/2003 Working Hours?
'Tek-Tips thread705-449121 for "Chargers"
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim Idx As Long
Dim StrtTim As Date
Dim EndTim As Date
Dim dtSt As Date
Dim dtEnd As Date
Dim TheDate As Date
Dim DlyHrs As Single
Dim strCriteria As String
Dim strSql As String
StrtTim = #8:30:00 AM#
EndTim = #5:00:00 PM#
DlyHrs = DateDiff("n", StrtTim, EndTim)
dtSt = Format(StDt, "Short Date")
dtEnd = Format(EndDt, "Short Date")
'Create an Array to hold the Time for Each Day
Dim MyDates() As MyDtHrsType
'Resize array for each DAY
' ReDim MyDates(dtEnd - dtSt + 1)
'Get Holidates
Set dbs = CurrentDb
strCriteria = "(HoliDate Between " & Chr(35) & dtSt & Chr(35) & _
" AND " & Chr(35) & dtEnd & Chr(35) & ")"
strSql = "Select Holidate "
strSql = strSql & "from tblHolidates "
strSql = strSql & "Where "
strSql = strSql & strCriteria & ";"
Set rst = dbs.OpenRecordset(strSql, dbOpenDynaset)
'Set Daily Hours for each Date
ReDim MyDates(0) 'Initalize the array
TheDate = dtSt
Idx = 1
While TheDate <= dtEnd
ReDim Preserve MyDates(UBound(MyDates) + 1)
'Insert the date
MyDates(Idx).MyDate = DateAdd("d", Idx - 1, dtSt)
'Check For Sat / Sun
If (Weekday(MyDates(Idx).MyDate) = vbSaturday Or _
Weekday(MyDates(Idx).MyDate) = vbSunday) Then
'Zero Hours for Weekend days
MyDates(Idx).MyHrs = 0
Else
'Check for First & last Days as Well as Holidates
If (Idx <> 1 Or Idx <> UBound(MyDates)) Then
'Not first / Last, Default Hrs to Daily Schedual
MyDates(Idx).MyHrs = DlyHrs
End If
End If
Idx = Idx + 1
TheDate = MyDates(Idx - 1).MyDate
Wend
'Initalize Start and End Date Times
MyDates(1).MyHrs = DateDiff("n", TimeValue(StDt), EndTim)
MyDates(UBound(MyDates)).MyHrs = DateDiff("n", StrtTim, TimeValue(EndDt))
Idx = 1
While Idx <= UBound(MyDates)
basDlyHrs = basDlyHrs + MyDates(Idx).MyHrs
Idx = Idx + 1
Wend
Do While Not rst.EOF
Idx = 1
While Idx <= UBound(MyDates)
If (MyDates(Idx).MyDate = rst!Holidate) Then
MyDates(Idx).MyHrs = 0
End If
Idx = Idx + 1
Wend
rst.MoveNext
Loop
basDlyHrs = basDlyHrs / 60
Set dbs = Nothing
End Function
(Posted yesterday in the wrong forum, put a note to refer here).
I have studied thread181-51747: Is calculating BUSINESS HOURS possible...?, thread705-1207464: Calculate business hours between two times, FAQ181-261: Calculate working days between two dates and anything related to the calculation of business hours, especially by Michael Red. Unfortunately, VBA is unknown to me and I am not a programmer. I got confused as to what code version was the appropriate one for our needs.
My company requires a way to calculate working hours and minutes between dates while removing weekends and holidays (coffee breaks and lunch calculation are not required). The results are to be stored in a separate table.
Assuming that:
Table:
"tblHolidays" contains [holidate] and [holiname] fields where [holiname] is "text" and [holidate] is "GeneralDate" to capture the HH:MM
"tblDates" contains [stdt] (StartDate) and [enddt] (End Date) where both fields are "GeneralDate"
The code below is saved in a module named "WH" (for working hours)
When I activate this code, it required a macro name, which I created to run this code: basDlyHrs («StDt», «EndDt») however an error occurs (cant find "stdt"
My questions are as follow:
Will the code below provide both business hours and min?
If yes, what am I doing wrong?
I do am sorry in advance if this is not clear enough however I think I did my best after a full day in trying to make this work. I envy your knowledge..
Thanks in advance
Public Function basDlyHrs(StDt As Date, EndDt As Date) As Double
'Michhael Red 1/1/2003 Working Hours?
'Tek-Tips thread705-449121 for "Chargers"
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim Idx As Long
Dim StrtTim As Date
Dim EndTim As Date
Dim dtSt As Date
Dim dtEnd As Date
Dim TheDate As Date
Dim DlyHrs As Single
Dim strCriteria As String
Dim strSql As String
StrtTim = #8:30:00 AM#
EndTim = #5:00:00 PM#
DlyHrs = DateDiff("n", StrtTim, EndTim)
dtSt = Format(StDt, "Short Date")
dtEnd = Format(EndDt, "Short Date")
'Create an Array to hold the Time for Each Day
Dim MyDates() As MyDtHrsType
'Resize array for each DAY
' ReDim MyDates(dtEnd - dtSt + 1)
'Get Holidates
Set dbs = CurrentDb
strCriteria = "(HoliDate Between " & Chr(35) & dtSt & Chr(35) & _
" AND " & Chr(35) & dtEnd & Chr(35) & ")"
strSql = "Select Holidate "
strSql = strSql & "from tblHolidates "
strSql = strSql & "Where "
strSql = strSql & strCriteria & ";"
Set rst = dbs.OpenRecordset(strSql, dbOpenDynaset)
'Set Daily Hours for each Date
ReDim MyDates(0) 'Initalize the array
TheDate = dtSt
Idx = 1
While TheDate <= dtEnd
ReDim Preserve MyDates(UBound(MyDates) + 1)
'Insert the date
MyDates(Idx).MyDate = DateAdd("d", Idx - 1, dtSt)
'Check For Sat / Sun
If (Weekday(MyDates(Idx).MyDate) = vbSaturday Or _
Weekday(MyDates(Idx).MyDate) = vbSunday) Then
'Zero Hours for Weekend days
MyDates(Idx).MyHrs = 0
Else
'Check for First & last Days as Well as Holidates
If (Idx <> 1 Or Idx <> UBound(MyDates)) Then
'Not first / Last, Default Hrs to Daily Schedual
MyDates(Idx).MyHrs = DlyHrs
End If
End If
Idx = Idx + 1
TheDate = MyDates(Idx - 1).MyDate
Wend
'Initalize Start and End Date Times
MyDates(1).MyHrs = DateDiff("n", TimeValue(StDt), EndTim)
MyDates(UBound(MyDates)).MyHrs = DateDiff("n", StrtTim, TimeValue(EndDt))
Idx = 1
While Idx <= UBound(MyDates)
basDlyHrs = basDlyHrs + MyDates(Idx).MyHrs
Idx = Idx + 1
Wend
Do While Not rst.EOF
Idx = 1
While Idx <= UBound(MyDates)
If (MyDates(Idx).MyDate = rst!Holidate) Then
MyDates(Idx).MyHrs = 0
End If
Idx = Idx + 1
Wend
rst.MoveNext
Loop
basDlyHrs = basDlyHrs / 60
Set dbs = Nothing
End Function