Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. Function officeWorkDay (startDay As Date, numWorkDays As Number) As Date
  2.  
  3. ' Identify the Monday before (or equal) firstDay
  4. Dim s As Number, d As Number, endDate As Date
  5. s = dayOfWeek(startDay, crMonday) - 1
  6.  
  7. ' Work out the resulting work day but not yet allowing for holidays
  8. If numWorkDays > 0 Then
  9. d = numWorkDays
  10. endDate = startDay - s ' move to beginning (Monday) of current week
  11. If (s >= 5) Then
  12. s = 4 ' Adjustment for start date is never more than 4 WD's
  13. End If
  14. d = d + s
  15.  
  16. endDate = endDate + (d \ 5) * 7 + d Mod 5
  17. Else
  18. If numWorkDays < 0 Then
  19. d = -1 * numWorkDays
  20. endDate = startDay + 4 - s ' move to friday of current week
  21. If (s >= 5) Then
  22. s = 5 ' Adjustment for start date is never more than 5 WD's
  23. End If
  24. d = d + 4 - s
  25.  
  26. endDate = endDate - (d \ 5) * 7 - d Mod 5
  27. Else
  28. endDate = startDay
  29. End If
  30. End If
  31.  
  32. ' Work out the number of holidays in the range
  33. '
  34. Dim numHolidayDays As Number
  35. If numWorkDays > 0 Then
  36. numHolidayDays = officeNumHolidaysIn(startDay, endDate)
  37. Else
  38. If numWorkDays < 0 Then
  39. numHolidayDays = officeNumHolidaysIn(endDate, startDay)
  40. End If
  41. End If
  42.  
  43. ' Now add this number of workdays onto the end of the range
  44. ' i.e. add the number of days but checking to ensure they're not holidays as well
  45. '
  46. Do While (numHolidayDays > 0)
  47. endDate = nextWeekDay(endDate, (numWorkDays > 0))
  48. If Not officeIsNonWorkingDay(endDate) Then
  49. numHolidayDays = numHolidayDays - 1
  50. End If
  51. Loop
  52.  
  53. ' Special case if we are adding 0 working days; make sure end date is a working day
  54. If numWorkDays = 0 Then
  55. endDate = officeThisOrNextWorkDay(endDate)
  56. End If
  57.  
  58. officeWorkDay = endDate
  59. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement