VBA for a date in a holiday list not working right

Lightkeepr

New Member
Joined
Apr 6, 2021
Messages
27
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Can you show us a small sample of your data and expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Can you show us a small sample of your data and expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Here you go. The dates in pink cells are listed as holidays in the named range, gray is a weekend date. I have also provided the results of using the function in column D, the WORKDAY formula in E and what the desired result is in column F if that helps.


Site Kit Delivery Schedule-NEW.xlsm
BCDEF
1DateCheckerWorkdayDesired Result
2Date Needed ByDelivery DateDelivery DateDelivery Date
3Thu, 07/14/2022Thu, 07/14/2022Thu, 07/14/2022Thu, 07/14/2022
4Fri, 07/15/2022Fri, 07/15/2022Fri, 07/15/2022Fri, 07/15/2022
5Sat, 07/16/2022Fri, 07/15/2022Mon, 07/18/2022Fri, 07/15/2022
6Sun, 07/17/2022Fri, 07/15/2022Mon, 07/18/2022Fri, 07/15/2022
7Mon, 07/18/2022Mon, 07/18/2022Mon, 07/18/2022Mon, 07/18/2022
8Tue, 07/19/2022Tue, 07/19/2022Tue, 07/19/2022Tue, 07/19/2022
9Wed, 07/20/2022Wed, 07/20/2022Fri, 07/22/2022Tue, 07/19/2022
10Thu, 07/21/2022Thu, 07/21/2022Fri, 07/22/2022Tue, 07/19/2022
11Fri, 07/22/2022Fri, 07/22/2022Fri, 07/22/2022Fri, 07/22/2022
12Sat, 07/23/2022Fri, 07/22/2022Mon, 07/25/2022Fri, 07/22/2022
13Sun, 07/24/2022Fri, 07/22/2022Mon, 07/25/2022Fri, 07/22/2022
14Mon, 07/25/2022Mon, 07/25/2022Mon, 07/25/2022Mon, 07/25/2022
15Tue, 07/26/2022Tue, 07/26/2022Tue, 07/26/2022Tue, 07/26/2022
16Wed, 07/27/2022Wed, 07/27/2022Wed, 07/27/2022Wed, 07/27/2022
17Thu, 07/28/2022Thu, 07/28/2022Thu, 07/28/2022Thu, 07/28/2022
18Fri, 07/29/2022Fri, 07/29/2022Fri, 07/29/2022Fri, 07/29/2022
19Sat, 07/30/2022Fri, 07/29/2022Mon, 08/01/2022Fri, 07/29/2022
20Sun, 07/31/2022Fri, 07/29/2022Mon, 08/01/2022Fri, 07/29/2022
21Mon, 08/01/2022Mon, 08/01/2022Mon, 08/01/2022Mon, 08/01/2022
22Tue, 08/02/2022Tue, 08/02/2022Tue, 08/02/2022Tue, 08/02/2022
23Wed, 08/03/2022Wed, 08/03/2022Wed, 08/03/2022Wed, 08/03/2022
Dates
Cell Formulas
RangeFormula
D3:D23D3=DateChecker(B3)
E3:E23E3=WORKDAY(B3-1,1,holidays)
Named Ranges
NameRefers ToCells
holidays=Holidays!$C$4:$C$39E3:E23
 
Upvote 0
See if this formula gets you what you need (with no VBA required):
Excel Formula:
=IF(WORKDAY(B3-1,1,holidays)=B3,B3,WORKDAY(B3-1,0,holidays))
 
Upvote 0
See if this formula gets you what you need (with no VBA required):
Excel Formula:
=IF(WORKDAY(B3-1,1,holidays)=B3,B3,WORKDAY(B3-1,0,holidays))

That works so long as there are not two holiday dates back to back as in this example. It corrects the 07/20/2022 Date Needed By to 7/19/2022 as expected but the 7/21/2022 Date Needed By calculates to 7/20/2022 which is a holiday.

This is why I was attempting to do this in VBA as I figured I could have the VBA adjust the date, check the adjusted against the holidays and if the adjusted date is on the list it would adjust it again until it's not on a holiday date or a weekend date.
 
Upvote 0
Can you post your holiday list, so I can set it up exactly the same way on my side, to ensure that I am testing the same data you are (I am not sure where you are, but those aren't holidays where I am located)?
 
Upvote 0
Can you post your holiday list, so I can set it up exactly the same way on my side, to ensure that I am testing the same data you are (I am not sure where you are, but those aren't holidays where I am located)?

Those dates are not recognized holidays, they are to simulate days the work site will be closed.

Site Kit Delivery Schedule-NEW.xlsm
CD
3DateHoliday
407/20/2022Site Closed
507/21/2022Site Closed
609/05/2022Labor Day
710/10/2022Columbus Day
811/11/2022Veterans Day
911/24/2022Thanksgiving
1011/25/2022Thanksgiving
1112/25/2022Christmas Day
1212/26/2022Christmas Day Recognized
1312/31/2022New Years Eve
1401/01/2023New Years Day
1501/16/2023MLK Day
1605/29/2023Memorial Day
1706/19/2023Juneteenth
1807/04/2023Independence Day
1909/04/2023Labor Day
2010/09/2023Columbus Day
2111/10/2023Veterans Day
2211/23/2023Thanksgiving
2311/24/2023Thanksgiving
2412/25/2023Christmas Day
2512/31/2023New Years Eve
2601/01/2024New Years Day
27
28
29
30
31
32
33
34
35
36
37
38
39
Holidays
 
Upvote 0
I see the conundrum now. I think you can extend the formula to check another level to get it to work, but then what happens if you have 3 in a row, etc.
So here is a simple User Defined Function that should do what you want:
VBA Code:
Function MyDateChecker(dtInput As Date) As Date

    Dim dt1 As Date
    Dim rngHol As Range
    
'   Set initial values
    dt1 = dtInput
    Set rngHol = ThisWorkbook.Worksheets("Holidays").Range("C5:C55")
    
'   Loop through days backwards until you find a valid weekday that is not a holiday
    Do
        If (Weekday(dt1, 2) < 6) And Application.WorksheetFunction.CountIf(rngHol, dt1) = 0 Then
            MyDateChecker = dt1
            Exit Do
        Else
            dt1 = dt1 - 1
        End If
    Loop

End Function
Basically, we just loop and keep subtracting one from the date until we get to a valid weekday that is not a holiday.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top