Yodelayheewho
Member
I am new at this and I am able to get the total count of days working, but I can't seem to exclude weekends and holidays.
The requirement is to count the days it takes to process an order. If the order is completed on the same day the PO is received, we do not count that day.
In this examples below, the PO is received on Friday, May 26th.
05/26/2023 = PO Rec'd
The next several lines show examples of entry dates and the results I need.
05/26/2023 = 0 (Same Weekday)
05/27/2023 = 0 (Saturday)
05/28/2023 = 0 (Sunday)
05/29/2023 = 0 (Holiday)
05/30/2023 = 1 (Weekday)
05/31/2023 = 2 (Weekday)
06/01/2023 = 3 (Weekday)
06/02/2023 = 4 (Weekday)
The next several lines show examples of the results I'm getting from the code below.
05/26/2023 = 0 (Same Weekday)
05/27/2023 = 1 (Saturday)
05/28/2023 = 2 (Sunday)
05/29/2023 = 3 (Holiday)
05/30/2023 = 4 (Weekday)
05/31/2023 = 5 (Weekday)
06/01/2023 = 6 (Weekday)
06/02/2023 = 7 (Weekday)
Also if the PO is Rec'd, but the date the order is processed is blank (because it hasn't been processed yet), I want the cell to remain blank. Instead, when I save the data from the userform to the worksheet, I get a 5-digit negative number, i.e., -45072
Your help would be greatly appreciated.
The requirement is to count the days it takes to process an order. If the order is completed on the same day the PO is received, we do not count that day.
In this examples below, the PO is received on Friday, May 26th.
05/26/2023 = PO Rec'd
The next several lines show examples of entry dates and the results I need.
05/26/2023 = 0 (Same Weekday)
05/27/2023 = 0 (Saturday)
05/28/2023 = 0 (Sunday)
05/29/2023 = 0 (Holiday)
05/30/2023 = 1 (Weekday)
05/31/2023 = 2 (Weekday)
06/01/2023 = 3 (Weekday)
06/02/2023 = 4 (Weekday)
The next several lines show examples of the results I'm getting from the code below.
05/26/2023 = 0 (Same Weekday)
05/27/2023 = 1 (Saturday)
05/28/2023 = 2 (Sunday)
05/29/2023 = 3 (Holiday)
05/30/2023 = 4 (Weekday)
05/31/2023 = 5 (Weekday)
06/01/2023 = 6 (Weekday)
06/02/2023 = 7 (Weekday)
Also if the PO is Rec'd, but the date the order is processed is blank (because it hasn't been processed yet), I want the cell to remain blank. Instead, when I save the data from the userform to the worksheet, I get a 5-digit negative number, i.e., -45072
Code:
Function ProcessDaysCount(x As Date, y As Date) As Long
Dim x As Date 'txtPORecDate
Dim y As Date 'txtSOAE10
Dim days As Long 'days between dates
Dim hol As Range 'list of holiday dates
x = DateValue.txtPORecDate
y = DateValue.txtSOAE10
days = x - y
hol = Application.Worksheets("LISTS").Range("S2:S")
If y = "" Then 'does not work, does not leave the cell blank
End If
Dim fullweek As Long 'identifies where the start and end date fall in a week
fullweek = days \ 7
Dim wkstart As Integer
Dim wkend As Integer
wkstart = Weekday(x)
wkend = Weekday(y)
Dim SatSun As Long 'does not work, weekend days are not subtracted
If days = (fullweek * 7) Then
SatSun = 0
ElseIf wkstart = vbSunday And wkend = vbSaturday Then
SatSun = 2
ElseIf wkstart = vbSunday Or wkend = vbSaturday Then
SatSun = 1
Else
SatSun = 0
End If
ProcessDaysCount = days - (fullweek * 2) - SatSun - hol
End Function