majid.pervaiz
Registered User.
- Local time
- Tomorrow, 01:23
- Joined
- Oct 15, 2012
- Messages
- 110
Dear Friends,
I have the below code which I get from this valuable forum, the below code is returning the total time time for each task within my department.
I want to understand why we use MOD function in this. I read in help and browse it says this will divide 2 numbers.
I am completely a new user, somehow I amended the code and it works fine, but I want to understand further so that in future I can do it myself and if any changes are required I will help myself.
Option Compare Database
Public Function NetWorkhours(dteStart As Date, dteEnd As Date, Spellout As Boolean) As Variant
Dim intGrossDays As Integer
Dim intGrossMins As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayMins As Single
Dim EndDayMins As Single
Dim NetworkMins As Integer
NetworkMins = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day
WorkDayStart = DateValue(dteEnd) + TimeValue("07:15:00")
WorkDayend = DateValue(dteStart) + TimeValue("14:30:00")
StartDayMins = DateDiff("n", dteStart, WorkDayend) '435
EndDayMins = DateDiff("n", WorkDayStart, dteEnd) '435
'adjust for time entries outside of business hours
'Calculate total hours and days between start and end times
intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossMins = DateDiff("n", (dteStart), (dteEnd))
'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbFriday) < 3 Then
nonWorkDays = nonWorkDays + 1
Else
'If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
'nonWorkDays = nonWorkDays + 1
'End If
End If
Next i
'Calculate number of work hours
Select Case intGrossDays
Case 0
'start and end time on same day
NetworkMins = (intGrossMins - ((nonWorkDays) * 1440))
Case 1
'start and end time on consecutive days
NetworkMins = StartDayMins + EndDayMins
Case Is > 1
'start and end time on non consecutive days
NetworkMins = (((intGrossDays - 1) - nonWorkDays) * 435) + (StartDayMins + EndDayMins)
End Select
If Spellout = True Then
NetWorkhours = MinsToTime(NetworkMins) ' hours and mins
Else
NetWorkhours = NetworkMins ' minutes only
End If
End Function
Function MinsToTime(Mins As Integer) As String
MinsToTime = Mins \ 60 & " hour" & IIf(Mins \ 60 <> 1, "s ", " ") & Mins Mod 60 & " minute" & IIf(Mins Mod 60 <> 1, "s", "")
End Function
I have the below code which I get from this valuable forum, the below code is returning the total time time for each task within my department.
I want to understand why we use MOD function in this. I read in help and browse it says this will divide 2 numbers.
I am completely a new user, somehow I amended the code and it works fine, but I want to understand further so that in future I can do it myself and if any changes are required I will help myself.
Option Compare Database
Public Function NetWorkhours(dteStart As Date, dteEnd As Date, Spellout As Boolean) As Variant
Dim intGrossDays As Integer
Dim intGrossMins As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayMins As Single
Dim EndDayMins As Single
Dim NetworkMins As Integer
NetworkMins = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day
WorkDayStart = DateValue(dteEnd) + TimeValue("07:15:00")
WorkDayend = DateValue(dteStart) + TimeValue("14:30:00")
StartDayMins = DateDiff("n", dteStart, WorkDayend) '435
EndDayMins = DateDiff("n", WorkDayStart, dteEnd) '435
'adjust for time entries outside of business hours
'Calculate total hours and days between start and end times
intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossMins = DateDiff("n", (dteStart), (dteEnd))
'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbFriday) < 3 Then
nonWorkDays = nonWorkDays + 1
Else
'If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
'nonWorkDays = nonWorkDays + 1
'End If
End If
Next i
'Calculate number of work hours
Select Case intGrossDays
Case 0
'start and end time on same day
NetworkMins = (intGrossMins - ((nonWorkDays) * 1440))
Case 1
'start and end time on consecutive days
NetworkMins = StartDayMins + EndDayMins
Case Is > 1
'start and end time on non consecutive days
NetworkMins = (((intGrossDays - 1) - nonWorkDays) * 435) + (StartDayMins + EndDayMins)
End Select
If Spellout = True Then
NetWorkhours = MinsToTime(NetworkMins) ' hours and mins
Else
NetWorkhours = NetworkMins ' minutes only
End If
End Function
Function MinsToTime(Mins As Integer) As String
MinsToTime = Mins \ 60 & " hour" & IIf(Mins \ 60 <> 1, "s ", " ") & Mins Mod 60 & " minute" & IIf(Mins Mod 60 <> 1, "s", "")
End Function