Include holidays in function code

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi EveryOne,
I want to include the holidays in the function code. The holidays details is in Sheet("Data") dynamic table TableNew A2:last row of data.
Please help !!

VBA Code:
Public Function IsWeekend(InputDate As Date) As Boolean
    Select Case Weekday(InputDate)
        Case vbSaturday, vbSunday
            IsWeekend = True
        Case Else
            IsWeekend = False
    End Select
End Function


Sub TAbsence13()
  Dim rng As Range
    Dim lastrow As Long
    Dim rngCol As Range
    Dim lastCol As Long
    
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set rng = Range("C1", Range("AL" & lastrow))
      
    For Each rngCol In rng.Columns
        rngCol.Cells(2).Font.Color = vbBlack

        If IsWeekend(rngCol.Cells(1)) = True Then
                rngCol.Cells(2).Font.Color = vbBlue
                  
        Else
                rngCol.Cells(2).Font.Color = vbBlack
        End If

    Next rngCol
    
 End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

In some cases questions are asked and solved multiple times. Hence it's always useful to search the forum to see if another users has a similar question which was solved.
With that intro it will be no surprise probably that is in this case also what happened.

See this and it will most probable needs some amending but it will give you direction, check this:

 
Upvote 0
Hi jorismoerings, I searched many articles about my quest including the one that you mentioned of whice I do not understand the whole logic thus not capable to correct it to suit my need. The one that I found and used seems much simple and easier to understand. How about amending like this ?

VBA Code:
Public Function IsWeekend(InputDate As Date) As Boolean
    Dim vbHoliday As Boolean
    Dim lastrowh As Long
        lastrowh = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
        vbHoliday = Application.Match(InputDate, ThisWorkbook.Worksheets("Data").Range("A2:A" & lastrowh).Value, 0)
    
    Select Case Weekday(InputDate)
        Case vbSaturday, vbSunday, vbHoliday
            IsWeekend = True
        Case Else
            IsWeekend = False
    End Select
            
End Function


Sub TAbsence13()
  Dim rng As Range
    Dim lastrow As Long
    Dim rngCol As Range
    Dim lastCol As Long
    
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set rng = Range("C1", Range("AL" & lastrow))
            
    For Each rngCol In rng.Columns
        rngCol.Cells(2).Font.Color = vbBlack

       'Debug.Print rngCol.Address
        If IsWeekend(rngCol.Cells(1)) = True Then
                rngCol.Cells(2).Font.Color = vbBlue
                  
        Else
                rngCol.Cells(2).Font.Color = vbBlack
        End If

    Next rngCol
    
 End Sub
 
Upvote 0
Hi Everyone,
Can anyone can help me to rectify the code.
VBA Code:
Option Explicit

Function FindWorkingDays(StartDate As Long, EndDate As Long, Optional InclSaturdays As Boolean = True, _
    Optional InclSundays As Boolean = True)

'Declaring variables
Dim RngFind As Range
Dim i As Long


For i = StartDate To EndDate
Set StartDate = ThisWorkbook.ActiveSheet.Range(1, 3)
Set EndDate = ThisWorkbook.ActiveSheet.Range(1, 38)

    
    On Error Resume Next
    
    'Finding the location where the specified date exist in the Holidays sheet
    Set RngFind = Worksheets("Data").Columns(1).Find(i)
    
    On Error GoTo 0
    
    'Checking whether it is holiday on the given date
    If Not RngFind Is Nothing Then
        GoTo ForLast
    End If
    
    'Checking whether it is Saturday on given date
    If InclSaturdays Then
        If Weekday(i, 2) = 6 Then
            GoTo ForLast
        End If
    End If
    
    'Checking whether it is Sunday on given date
    If InclSundays Then
        If Weekday(i, 2) = 7 Then
            GoTo ForLast
        End If
    End If
    
    FindWorkingDays = FindWorkingDays
ForLast:

Next

End Function
Sub FindWeekendCol14()
  Dim rng As Range
    Dim lastrow As Long
    Dim rngCol As Range
    Dim lastCol As Long
    
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set rng = Range("C1", Range("AL" & lastrow))
            
    For Each rngCol In rng.Columns
        rngCol.Cells(2).Font.Size = 12
      
        If FindWorkingDays(rngCol.Cells(1)) = True Then
                rngCol.Cells(2).Font.Size = 16
        Else
                rngCol.Cells(2).Font.Size = 12
        End If

    Next rngCol
    
 End Sub
 

Attachments

  • arg.png
    arg.png
    73.7 KB · Views: 6
Upvote 0
Hi,

Keep posting changed VBA scripts without context on what you're trying to do, isn't the way to get assistance.

If you could provide a part of your file (using XL2BB) or copy of a file (no sensitive data) that shows the problem by uploading to a file-share site and provide a link here that might enable us to investigate better.
 
Upvote 0
This shows no error but does not work
Code:
Option Explicit

Function FindWorkingDays(InputDate As Long, Optional InclSaturdays As Boolean = True, _
    Optional InclSundays As Boolean = True)

'Declaring variables
Dim RngFind As Range

    On Error Resume Next

    Dim lastrowh As Long
       lastrowh = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
   
    'Finding the location where the specified date exist in the Holidays sheet
     Set RngFind = Worksheets("Data").Range("A2:A" & lastrowh).Find(InputDate)
   
    On Error GoTo 0
   
    'Checking whether it is holiday on the given date
    If Not RngFind Is Nothing Then
        GoTo ForLast
    End If
   
    'Checking whether it is Saturday on given date
    If InclSaturdays Then
        If Weekday(InputDate, 2) = 6 Then
            GoTo ForLast
        End If
    End If
   
    'Checking whether it is Sunday on given date
    If InclSundays Then
        If Weekday(InputDate, 2) = 7 Then
            GoTo ForLast
        End If
    End If
   
ForLast:
End Function


Sub FindWeekendCol14()
  Dim rng As Range
    Dim lastrow As Long
    Dim rngCol As Range
    Dim lastCol As Long
   
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
   
    Set rng = Range("C1", Range("AL" & lastrow))
   
    For Each rngCol In rng.Columns
        rngCol.Cells(2).Font.Size = 12
   
        If FindWorkingDays(rngCol.Cells(1)) = True Then
               rngCol.Cells(2).Font.Size = 16
        Else
               rngCol.Cells(2).Font.Size = 12
        End If

    Next rngCol
   
 End Sub
Hi,

Keep posting changed VBA scripts without context on what you're trying to do, isn't the way to get assistance.

If you could provide a part of your file (using XL2BB) or copy of a file (no sensitive data) that shows the problem by uploading to a file-share site and provide a link here that might enable us to investigate better.
RosterWebv.xlsm
A
1Holidays (A2:End)
2January 1, 2021
3February 12, 2021
4February 13, 2021
5February 15, 2021
6April 2, 2021
7April 3, 2021
8April 5, 2021
9May 1, 2021
10May 19, 2021
11June 14, 2021
12July 1, 2021
13July 5, 2021
14September 22, 2021
15October 1, 2021
16October 14, 2021
17December 25, 2021
18December 27, 2021
19January 1, 2022
20February 1, 2022
21February 2, 2022
22February 3, 2022
23November 25, 2021
Data



RosterWebv.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABB
1MDateAttendance26-Jun27-Jun28-Jun29-Jun30-Jun1-Jul2-Jul3-Jul4-Jul5-Jul6-Jul7-Jul8-Jul9-Jul10-Jul11-Jul12-Jul13-Jul14-Jul15-Jul16-Jul17-Jul18-Jul19-Jul20-Jul21-Jul22-Jul23-Jul24-Jul25-Jul26-Jul27-Jul28-Jul29-Jul30-Jul31-JulTotal DTotal D4Total GTotal D2Total ETotal NTotal OTotal ALTotal AMTotal PMTotal MITotal LETotal ATTNTotal WeekendsTotal HolidaysTotal Workdays
2DateSummary(5)(4)(3)(2)(1)12345678910111213141516171819202122232425262728293031DD4GD2ENOALAMPMMIColumn1ATTNWeekendsHolidaysWorkDays
3Cat G#NAME?00000000000009220
4Mathew Y#NAME?00000000000009220
5John W#NAME?00000000000009220
6Robert H#NAME?00000000000009220
7Man L#NAME?00000000000009220
8Vincent M#NAME?00000000000009220
9Jeffrey L#NAME?00000000000009220
10Zita Y#NAME?00000000000009220
11Cat G#NAME?00000000000009220
12Jason J#NAME?00000000000009220
13Vince M#NAME?00000000000009220
14Roy L#NAME?00000000000009220
202107
Cell Formulas
RangeFormula
AY1,AQ1:AW1,AM1:AO1AM1="Total "&AM2
AW3:AW14,AM3:AT14AM3=COUNTIF($H3:$AL3,AM$2)
AU3:AV14AU3=COUNTIF($H3:$AL3,AU$2)/2
AX3:AX14AX3=SUM(Table2[@[AL]:[MI]])
AY3:AY14AY3=SUM(AM3:AW3)
AZ3:AZ14AZ3=(EOMONTH(H$1,0)-H$1)-NETWORKDAYS(H$1,EOMONTH(H$1,0))+1
BA3:BA14BA3=NETWORKDAYS(H$1,EOMONTH(H$1,0))-NETWORKDAYS(H$1,EOMONTH(H$1,0),TableNew[Holidays (A2:End)])
BB3:BB14BB3=NETWORKDAYS(H$1,EOMONTH(H$1,0),TableNew[Holidays (A2:End)])
B3:B14B3=_xlfn.TEXTJOIN(,TRUE,"T:"&BB3," ","L:",SUM(AT3:AW3), " ","D:",SUM(AM3:AP3)," ","E:",AQ3," ","N:",AR3)
Named Ranges
NameRefers ToCells
HolidaysNew=TableNew[Holidays (A2:End)]BA3:BB14
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3:B14Expression=SUM(AM3:AR3)<BB3textNO
C1:AL14Expression=WEEKDAY(C$1,2)>5textNO
C1:AL14Expression=COUNTIF(HolidaysNew,C$1)textNO
B3Expression=SUM(AM3:AR3)>BB3textNO
G3,E3:F14Expression=SUM(AL3:AQ3)>BA3textNO
G4:G14Expression=SUM(AN4:AS4)>BC4textNO
BA14Cell Value>0textNO
BA1,BA3:BA14Cell Value>0textNO
B4:B14Expression=SUM(AM4:AR4)>BB4textNO
C3:D14Expression=SUM(AJ3:AN3)>AY3textNO
Cells with Data Validation
CellAllowCriteria
H3:AL14List=ShiftcodeNew
A3:A13List=HelpAgent
A14List=HelpAgent
 
Upvote 0
Hello Vincent 88,
according to your first post I understand you want to highlight columns if they begin with weekend or
they begin with some of holydays specified in the column "A".
If I'm right you can try this...

VBA Code:
Public Function IsWeekend(InputDate As Date) As Boolean

    Dim vLastRow As Long
    Dim vR1 As Range

    With ThisWorkbook.Worksheets("Data")
        vLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For Each vR1 In .Range("A2:A" & vLastRow)
            If Day(InputDate) = Day(vR1) And _
               Month(InputDate) = Month(vR1) Or _
               Weekday(InputDate) = 1 Or _
               Weekday(InputDate) = 7 Then
               IsWeekend = True
               Exit Function
            Else
                IsWeekend = False
            End If
        Next vR1
     End With
     
End Function


Sub TAbsence13()

    Dim vRng As Range
    Dim vLastRow As Long
    Dim vRngCol As Range
    
    With ThisWorkbook.Worksheets("Data")
        vLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set vRng = Range("C1", Range("AL" & vLastRow))
        For Each vRngCol In vRng.Columns
            If IsWeekend(vRngCol.Cells(1)) = True Then
                With Columns(vRngCol.Column)
                    .Font.Color = vbBlue
                End With
            Else
                With Columns(vRngCol.Column)
                   .Font.Color = vbBlack
                End With
            End If
        Next vRngCol
    End With
    
 End Sub
 
Upvote 0
Solution
Hi EXCEL MAX,
Thank you for code. In fact there are two sheets to work with. One sheet "Data" contains the holidays and the other is the calendar sheet to apply the module, I changed
With ThisWorkbook.Worksheets("Data") to With ThisWorkbook.Activesheet to make it works.
Based on your advice, I work further to apply worksheetfunction to it but encounter difficulty, I post my quest in another thread (Apply worksheet function in module). Hope you can help again. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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