Hi, I have written a function to calculate the number of minutes elapsed between two dates (datediff) and also drop non working days (sat/sun + holidays).
The trouble is I use this function daily in queries to calcualte daily turnaround, but it is causing massive slow down on my PC. The PC is a p3 700 with 128 ram, is there anything that i can do to optimize my code (quite new tp coding - so there may be an easier way of doinf what i have done).
I have attached my code.....
Public Function NonWorkingDays(DateIn, DateOut)
On Error GoTo CalcError:
Dim TotalHours As Integer
Dim HoursToDrop As Integer
Dim StartDate As Date
Dim EndDate As Date
Dim Monday As Date
Dim Newstring As String
Dim DateCounter As Date
Dim HoliCount As Long
Dim HoliStart As Date
Dim HoliEnd As Date
Dim HoliDays As Long
StartDate = Format(DateIn, "general date"
EndDate = Format(DateOut, "general date"
TotalHours = DateDiff("n", StartDate, EndDate)
HoursToDrop = 0
'if received and completed on sat or sun then calc hours only
If WeekDay(StartDate) = vbSunday And WeekDay(EndDate) = vbSunday And TotalHours < 1440 Then
NonWorkingDays = Format(TotalHours / 60, "0.0"
Exit Function
ElseIf WeekDay(StartDate) = vbSaturday And WeekDay(EndDate) = vbSaturday Or WeekDay(EndDate) = vbSunday And TotalHours < 2880 Then
NonWorkingDays = Format(TotalHours / 60, "0.0"
Exit Function
Else
End If
'startdate convert to monday morning at 09:00 if received + completed on either Sat or Sun
If WeekDay(StartDate) = vbSunday Then
Newstring = StartDate + 1
Newstring = Left(Newstring, 10) & " 08:00"
Monday = Newstring
HoursToDrop = DateDiff("n", StartDate, Monday)
ElseIf WeekDay(StartDate) = vbSaturday Then
Newstring = StartDate + 2
Newstring = Left(Newstring, 10) & " 08:00"
Monday = Newstring
HoursToDrop = DateDiff("n", StartDate, Monday)
Else
End If
'drop all sat and sun between startdate and end date
If Newstring = "" Then
DateCounter = StartDate
Do While DateCounter < EndDate
If WeekDay(DateCounter) = vbSaturday Then
HoursToDrop = HoursToDrop + 1440
ElseIf WeekDay(DateCounter) = vbSunday Then
HoursToDrop = HoursToDrop + 1440
End If
DateCounter = DateCounter + 1
Loop
Else
DateCounter = Newstring
Do While DateCounter < EndDate
If WeekDay(DateCounter) = vbSaturday Then
HoursToDrop = HoursToDrop + 1440
ElseIf WeekDay(DateCounter) = vbSunday Then
HoursToDrop = HoursToDrop + 1440
End If
DateCounter = DateCounter + 1
Loop
End If
'check for holidays in the start and end date period (based on the holidays table)then convert to hours to drop
HoliCount = 0
HoliStart = Left(StartDate, 10)
HoliEnd = Left(EndDate, 10)
Do While HoliStart < HoliEnd
If WeekDay(HoliStart) = vbSaturday Or WeekDay(HoliStart) = vbSunday Then
Else
HoliDays = DCount("holidate", "HolidayTable", "holidate = #" & HoliStart & "#"
HoliCount = HoliCount + HoliDays
End If
HoliStart = HoliStart + 1
Loop
HoliCount = HoliCount * 1440
HoursToDrop = HoursToDrop + HoliCount
'true working hours is total hours minus the hours to be dropped - and there you go !
NonWorkingDays = TotalHours - HoursToDrop
NonWorkingDays = Format(NonWorkingDays / 60, "0.0"
Exit Function
CalcError:
If Err.number = 94 Then
NonWorkingDays = Null
End If
End Function
Any ideas wuold be very helpful thanks......
The trouble is I use this function daily in queries to calcualte daily turnaround, but it is causing massive slow down on my PC. The PC is a p3 700 with 128 ram, is there anything that i can do to optimize my code (quite new tp coding - so there may be an easier way of doinf what i have done).
I have attached my code.....
Public Function NonWorkingDays(DateIn, DateOut)
On Error GoTo CalcError:
Dim TotalHours As Integer
Dim HoursToDrop As Integer
Dim StartDate As Date
Dim EndDate As Date
Dim Monday As Date
Dim Newstring As String
Dim DateCounter As Date
Dim HoliCount As Long
Dim HoliStart As Date
Dim HoliEnd As Date
Dim HoliDays As Long
StartDate = Format(DateIn, "general date"
EndDate = Format(DateOut, "general date"
TotalHours = DateDiff("n", StartDate, EndDate)
HoursToDrop = 0
'if received and completed on sat or sun then calc hours only
If WeekDay(StartDate) = vbSunday And WeekDay(EndDate) = vbSunday And TotalHours < 1440 Then
NonWorkingDays = Format(TotalHours / 60, "0.0"
Exit Function
ElseIf WeekDay(StartDate) = vbSaturday And WeekDay(EndDate) = vbSaturday Or WeekDay(EndDate) = vbSunday And TotalHours < 2880 Then
NonWorkingDays = Format(TotalHours / 60, "0.0"
Exit Function
Else
End If
'startdate convert to monday morning at 09:00 if received + completed on either Sat or Sun
If WeekDay(StartDate) = vbSunday Then
Newstring = StartDate + 1
Newstring = Left(Newstring, 10) & " 08:00"
Monday = Newstring
HoursToDrop = DateDiff("n", StartDate, Monday)
ElseIf WeekDay(StartDate) = vbSaturday Then
Newstring = StartDate + 2
Newstring = Left(Newstring, 10) & " 08:00"
Monday = Newstring
HoursToDrop = DateDiff("n", StartDate, Monday)
Else
End If
'drop all sat and sun between startdate and end date
If Newstring = "" Then
DateCounter = StartDate
Do While DateCounter < EndDate
If WeekDay(DateCounter) = vbSaturday Then
HoursToDrop = HoursToDrop + 1440
ElseIf WeekDay(DateCounter) = vbSunday Then
HoursToDrop = HoursToDrop + 1440
End If
DateCounter = DateCounter + 1
Loop
Else
DateCounter = Newstring
Do While DateCounter < EndDate
If WeekDay(DateCounter) = vbSaturday Then
HoursToDrop = HoursToDrop + 1440
ElseIf WeekDay(DateCounter) = vbSunday Then
HoursToDrop = HoursToDrop + 1440
End If
DateCounter = DateCounter + 1
Loop
End If
'check for holidays in the start and end date period (based on the holidays table)then convert to hours to drop
HoliCount = 0
HoliStart = Left(StartDate, 10)
HoliEnd = Left(EndDate, 10)
Do While HoliStart < HoliEnd
If WeekDay(HoliStart) = vbSaturday Or WeekDay(HoliStart) = vbSunday Then
Else
HoliDays = DCount("holidate", "HolidayTable", "holidate = #" & HoliStart & "#"
HoliCount = HoliCount + HoliDays
End If
HoliStart = HoliStart + 1
Loop
HoliCount = HoliCount * 1440
HoursToDrop = HoursToDrop + HoliCount
'true working hours is total hours minus the hours to be dropped - and there you go !
NonWorkingDays = TotalHours - HoursToDrop
NonWorkingDays = Format(NonWorkingDays / 60, "0.0"
Exit Function
CalcError:
If Err.number = 94 Then
NonWorkingDays = Null
End If
End Function
Any ideas wuold be very helpful thanks......