againofsoul
New Member
- Joined
- May 18, 2024
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
Hi I am facing difficult in getting the correct enddate time.
Please refer to below vba. Those in Red are populating the wrong enddatetime. Can help to check my vba function?
This are the criteria:
1: Working hours for Weekday 8.30 am to 5.30 pm excluding lunch break from 12 pm to 1 pm
2: Working hours for Weekends / PH is from 8.30 am to 12.30 pm (no lunch)
My intention is to get the enddatetime if X no. of working hours is added to the startdatetime.
Function AddWorkingHours(startDate As Date, hoursToAdd As Integer) As Date
Dim currentDateTime As Date
Dim hoursRemaining As Integer
Dim lunchStart As Date
Dim lunchEnd As Date
Dim publicHolidays As Collection
Dim holiday As Variant
' Check if the current date is a public holiday
Dim isHoliday As Boolean
isHoliday = False
' Define public holidays
Set publicHolidays = New Collection
publicHolidays.Add DateSerial(2024, 1, 1) ' 01-Jan-2024
publicHolidays.Add DateSerial(2024, 2, 11) ' 11-Feb-2024
publicHolidays.Add DateSerial(2024, 2, 12) ' 12-Feb-2024
publicHolidays.Add DateSerial(2024, 3, 29) ' 29-Mar-2024
publicHolidays.Add DateSerial(2024, 4, 10) ' 10-Apr-2024
publicHolidays.Add DateSerial(2024, 5, 1) ' 01-May-2024
publicHolidays.Add DateSerial(2024, 5, 22) ' 22-May-2024
publicHolidays.Add DateSerial(2024, 6, 17) ' 17-Jun-2024
publicHolidays.Add DateSerial(2024, 8, 9) ' 09-Aug-2024
publicHolidays.Add DateSerial(2024, 10, 31) ' 31-Oct-2024
publicHolidays.Add DateSerial(2024, 12, 25) ' 25-Dec-2024
For Each holiday In publicHolidays
If DateValue(currentDateTime) = holiday Then
isHoliday = True
Exit For
End If
Next holiday
If startDate = isHoliday Or Weekday(startDate, vbMonday) > 5 Then
If TimeValue(startDate) > TimeValue("12:30:00") Then
startDate = DateValue(startDate) + TimeValue("12:30:00")
End If
End If
If startDate = isHoliday Or Weekday(startDate, vbMonday) > 5 Then
If TimeValue(startDate) < TimeValue("8:30:00") Then
startDate = DateValue(startDate) + TimeValue("8:30:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) < TimeValue("8:30:00") Then
startDate = DateValue(startDate) + TimeValue("8:30:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) < TimeValue("13:00:00") And TimeValue(startDate) > TimeValue("11:59:59") Then
startDate = DateValue(startDate) + TimeValue("13:00:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) > TimeValue("17:30:00") Then
startDate = DateValue(startDate) + TimeValue("17:30:00")
End If
End If
currentDateTime = startDate
hoursRemaining = hoursToAdd
Do While hoursRemaining > 0
' Define lunch break times
lunchStart = TimeSerial(12, 0, 0)
lunchEnd = TimeSerial(13, 0, 0)
' Add one hour to the current datetime
currentDateTime = currentDateTime + TimeValue("01:00:00")
If currentDateTime = isHoliday Then
If Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12.5 Then
' Decrease the remaining hours to add
hoursRemaining = hoursRemaining - 1
End If
ElseIf Weekday(currentDateTime, vbMonday) < 6 And Not isHoliday Then ' Regular weekday
If (Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12) Or (Hour(currentDateTime) >= 13 And Hour(currentDateTime) < 17.5) Then
' Exclude lunch break
hoursRemaining = hoursRemaining - 1
End If
ElseIf Weekday(currentDateTime, vbMonday) > 5 And Not isHoliday Then
If Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12.5 Then
' Decrease the remaining hours to add
hoursRemaining = hoursRemaining - 1
End If
End If
Loop
If Hour(currentDateTime) = 13 And Minute(currentDateTime) = 0 Then
currentDateTime = DateValue(currentDateTime) + TimeValue("12:00:00")
End If
' Return the final datetime
AddWorkingHours = currentDateTime
End Function
Please refer to below vba. Those in Red are populating the wrong enddatetime. Can help to check my vba function?
This are the criteria:
1: Working hours for Weekday 8.30 am to 5.30 pm excluding lunch break from 12 pm to 1 pm
2: Working hours for Weekends / PH is from 8.30 am to 12.30 pm (no lunch)
My intention is to get the enddatetime if X no. of working hours is added to the startdatetime.
Function AddWorkingHours(startDate As Date, hoursToAdd As Integer) As Date
Dim currentDateTime As Date
Dim hoursRemaining As Integer
Dim lunchStart As Date
Dim lunchEnd As Date
Dim publicHolidays As Collection
Dim holiday As Variant
' Check if the current date is a public holiday
Dim isHoliday As Boolean
isHoliday = False
' Define public holidays
Set publicHolidays = New Collection
publicHolidays.Add DateSerial(2024, 1, 1) ' 01-Jan-2024
publicHolidays.Add DateSerial(2024, 2, 11) ' 11-Feb-2024
publicHolidays.Add DateSerial(2024, 2, 12) ' 12-Feb-2024
publicHolidays.Add DateSerial(2024, 3, 29) ' 29-Mar-2024
publicHolidays.Add DateSerial(2024, 4, 10) ' 10-Apr-2024
publicHolidays.Add DateSerial(2024, 5, 1) ' 01-May-2024
publicHolidays.Add DateSerial(2024, 5, 22) ' 22-May-2024
publicHolidays.Add DateSerial(2024, 6, 17) ' 17-Jun-2024
publicHolidays.Add DateSerial(2024, 8, 9) ' 09-Aug-2024
publicHolidays.Add DateSerial(2024, 10, 31) ' 31-Oct-2024
publicHolidays.Add DateSerial(2024, 12, 25) ' 25-Dec-2024
For Each holiday In publicHolidays
If DateValue(currentDateTime) = holiday Then
isHoliday = True
Exit For
End If
Next holiday
If startDate = isHoliday Or Weekday(startDate, vbMonday) > 5 Then
If TimeValue(startDate) > TimeValue("12:30:00") Then
startDate = DateValue(startDate) + TimeValue("12:30:00")
End If
End If
If startDate = isHoliday Or Weekday(startDate, vbMonday) > 5 Then
If TimeValue(startDate) < TimeValue("8:30:00") Then
startDate = DateValue(startDate) + TimeValue("8:30:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) < TimeValue("8:30:00") Then
startDate = DateValue(startDate) + TimeValue("8:30:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) < TimeValue("13:00:00") And TimeValue(startDate) > TimeValue("11:59:59") Then
startDate = DateValue(startDate) + TimeValue("13:00:00")
End If
End If
If startDate <> isHoliday And Weekday(startDate, vbMonday) < 6 Then
If TimeValue(startDate) > TimeValue("17:30:00") Then
startDate = DateValue(startDate) + TimeValue("17:30:00")
End If
End If
currentDateTime = startDate
hoursRemaining = hoursToAdd
Do While hoursRemaining > 0
' Define lunch break times
lunchStart = TimeSerial(12, 0, 0)
lunchEnd = TimeSerial(13, 0, 0)
' Add one hour to the current datetime
currentDateTime = currentDateTime + TimeValue("01:00:00")
If currentDateTime = isHoliday Then
If Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12.5 Then
' Decrease the remaining hours to add
hoursRemaining = hoursRemaining - 1
End If
ElseIf Weekday(currentDateTime, vbMonday) < 6 And Not isHoliday Then ' Regular weekday
If (Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12) Or (Hour(currentDateTime) >= 13 And Hour(currentDateTime) < 17.5) Then
' Exclude lunch break
hoursRemaining = hoursRemaining - 1
End If
ElseIf Weekday(currentDateTime, vbMonday) > 5 And Not isHoliday Then
If Hour(currentDateTime) >= 8.5 And Hour(currentDateTime) < 12.5 Then
' Decrease the remaining hours to add
hoursRemaining = hoursRemaining - 1
End If
End If
Loop
If Hour(currentDateTime) = 13 And Minute(currentDateTime) = 0 Then
currentDateTime = DateValue(currentDateTime) + TimeValue("12:00:00")
End If
' Return the final datetime
AddWorkingHours = currentDateTime
End Function