Lightkeepr
New Member
- Joined
- Apr 6, 2021
- Messages
- 27
- Office Version
- 365
- Platform
- Windows
I have the following VBA code that the first part where it looks to see if the date is a weekend date works great. It adjusts the dates backwards to the next available Mon-Fri date. The second step is for it to check the date against a list of dates that are holidays. This step doesn't seem to be doing anything and I cannot figure out why. I have it looping three time so that it checks to make sure that the new adjusted date (if one was found on the list) is not also a holiday on the list. Any ideas of where I'm missing something? In the worksheet that I am using this, column A has the ideal date for a delivery, in column D I have the formula =DateChecker(A5) to give me the adjusted date based on weekends/holidays off.
VBA Code:
Function DateChecker(ByVal dateIO As Date) As Date
Dim SearchRng As Range
Dim DateSearch As String
Dim resMatch As Variant
Dim LoopCheck As Integer
' Application.Volatile True
Set SearchRng = ThisWorkbook.Worksheets("Holidays").Range("C5:C55")
'Check if date is weekend and adjust it
If Weekday(dateIO, 2) = 7 Then
dateIO = dateIO - 2
ElseIf Weekday(dateIO, 2) = 6 Then
dateIO = dateIO - 1
End If
LoopCheck = 3
'check date against the holidays list
For LoopCheck = 1 To LoopCheck
DateSearch = dateIO
resMatch = Application.Match(DateSearch, SearchRng, 0)
If IsError(resMatch) Then
GoTo Last
Else
dateIO = dateIO - 1
End If
Next LoopCheck
Last:
'Check if final date is weekend and adjust it
If Weekday(dateIO, 2) = 7 Then
dateIO = dateIO - 2
ElseIf Weekday(dateIO, 2) = 6 Then
dateIO = dateIO - 1
End If
DateChecker = dateIO
End Function