Originally Posted by
June7
You show date inputs in international format instead of Access structure. Review
http://allenbrowne.com/ser-36.html
Learn debugging techniques, refer to link at bottom of my post. Step debug.
Sorry, error in seconds calculation. I thought sure had it right in that other thread.
intHours = intTotalSec \ 3600 'hours
intMinutes = Format(Int((intTotalSec Mod 3600) / 60), "00") 'minutes
intSeconds = Format(intTotalSec - (intHours * 3600 + intMinutes * 60), "00") 'seconds
'return value
WorkingHrsMinsSecs = intHours & ":" & intMinutes & ":" & intSeconds
thanks,
I change the code, as past below and for the first example it became correct 0:3:36
But for the other two examples it contine to give error, itīs not possivel do have hours entry on the 8:00PM to 8:00AM
Code:
Public Function WorkingHrsMinsSecs(StartDate As Date, EndDate As Date) As String
'....................................................................
' Name: WorkingHrs
' Inputs: StartDate As Date & time
' EndDate As Date & time
' Returns: Integer - number of hours not inclusive of weekends
' Author: Arvin Meyer
' Date: February 19, 1997
' Modified by Steve S (ssanfu)
' Date: April 11, 2012
' Comment: Accepts two dates and returns the number
' of work hours and minutes between 8am and 8pm
' Note that this function does not account for holidays.
'....................................................................
On Error GoTo Err_WorkingHrs
Const cBeginingTime As Date = #8:00:00 AM#
Const cEndingTime As Date = #8:00:00 PM#
Dim intCountDays As Integer
Dim intHours As Integer
Dim intMinutes As Integer
Dim intTotalMin As Integer
Dim intTotalSec As Integer
Dim dtStart As Date
Dim dtEnd As Date
Dim dtTemp As Date
Dim tmStart As Date
Dim tmEnd As Date
intMinutes = 0
intHours = 0
intSeconds = 0
intTotalSec = 0
intCountDays = 0
intTotalMin = 0
WorkingHrsMinsSec = 0
'check end date > start date
If EndDate < StartDate Then
dtTemp = StartDate
StartDate = EndDate
EndDate = dtTemp
End If
'get just the date portion
dtStart = Int(StartDate)
dtEnd = Int(EndDate)
'get just the time portion
tmStart = StartDate - dtStart
tmEnd = EndDate - dtEnd
'check start and end times are valid
If Not (tmStart >= cBeginingTime And tmStart <= cEndingTime) Then
MsgBox "Invalid start time. Start time before " & cBeginingTime
Exit Function
ElseIf Not (tmEnd >= cBeginingTime And tmEnd <= cEndingTime) Then
MsgBox "Invalid end time. End time after " & cEndingTime
Exit Function
End If
If dtStart = dtEnd Then
intTotalSec = DateDiff("s", tmStart, tmEnd)
Else
'skip the first day
dtStart = dtStart + 1
Do While dtStart < dtEnd
'Make the above < and not <= to not count the EndDate
Select Case Weekday(dtStart)
Case Is = 1, 7
'do nothing
Case Is = 2, 3, 4, 5, 6
intCountDays = intCountDays + 1
End Select
dtStart = dtStart + 1
Loop
intTotalSec = intCountDays * 720 * 60
'first day minutes
intTotalSec = intTotalSec + DateDiff("s", tmStart, cEndingTime)
'Last day minutes
intTotalSec = intTotalSec + DateDiff("s", cBeginingTime, tmEnd)
End If
intHours = intTotalSec \ 3600 'hours
intMinutes = Format(Int((intTotalSec Mod 3600) / 60), "00") 'minutes
intSeconds = Format(intTotalSec - (intHours * 3600 + intMinutes * 60), "00") 'seconds
'return value
WorkingHrsMinsSecs = intHours & ":" & intMinutes & ":" & intSeconds
Exit_WorkingHrs:
Exit Function
Err_WorkingHrs:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingHrs
End Select
End Function