Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function HoldayTaken(Employee As String, StartDate As Date, EndDate As Date) As Integer
- Application.Volatile True
- Dim ExclDates
- 'We Grab the year from our records to keep it current
- NewYearDay = "01/01"
- Christmas = "25/12"
- Dim tbl As ListObject
- Set tbl = Sheets("DataSheet").ListObjects("Holidays")
- Dim Enabled_ As Boolean
- Enabled_ = True
- With tbl
- For i = 1 To .ListRows.Count
- If .DataBodyRange(i, 1).Value = Employee Then
- 'If Holday StartDate is within Range
- If .DataBodyRange(i, 2).Value >= StartDate And .DataBodyRange(i, 2).Value <= EndDate Then
- 'If Holiday EndDate is within Range
- If .DataBodyRange(i, 3).Value >= StartDate And .DataBodyRange(i, 3).Value <= EndDate Then
- 'Return Days
- Tally = Tally + DateDiff("d", .DataBodyRange(i, 2).Value, .DataBodyRange(i, 3).Value)
- 'If Holiday EndDate is out of range
- ElseIf .DataBodyRange(i, 3).Value >= EndDate Then
- Tally = Tally + DateDiff("d", .DataBodyRange(i, 2).Value, EndDate)
- End If
- 'Apply 1 to date
- If Enabled_ Then
- Tally = Tally + 1
- Enabled_ = False
- End If
- 'If Holiday covers the entire week
- ElseIf .DataBodyRange(i, 3).Value < StartDate And .DataBodyRange(i, 3).Value > EndDate Then
- Tally = Tally + DateDiff("d", StartDate, EndDate)
- 'Apply 1 to date
- If Enabled_ Then
- Tally = Tally + 1
- Enabled_ = False
- End If
- 'If Holiday StartDate is less than Week StartDate
- ElseIf .DataBodyRange(i, 2).Value < StartDate Then
- If .DataBodyRange(i, 3).Value >= StartDate And .DataBodyRange(i, 3).Value <= EndDate Then
- Tally = Tally + DateDiff("d", StartDate, .DataBodyRange(i, 3).Value)
- 'Apply 1 to date
- If Enabled_ Then
- Tally = Tally + 1
- Enabled_ = False
- End If
- Else
- Tally = Tally + 0
- End If
- Else
- Tally = Tally + 0
- End If
- If DateValue(Christmas & "/" & Year(.DataBodyRange(i, 2).Value)) >= StartDate And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 2).Value)) <= EndDate Then
- If DateValue(Christmas & "/" & Year(.DataBodyRange(i, 2).Value)) >= .DataBodyRange(i, 2).Value And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) <= .DataBodyRange(i, 2).Value Then
- Tally = Tally - 1
- End If
- ElseIf DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) >= StartDate And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) <= EndDate Then
- If DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) >= .DataBodyRange(i, 2).Value And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) <= .DataBodyRange(i, 3).Value Then
- Tally = Tally - 1
- End If
- End If
- If DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) >= StartDate And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) <= EndDate Then
- If DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) >= .DataBodyRange(i, 2).Value And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) <= .DataBodyRange(i, 3).Value Then
- Tally = Tally - 1
- End If
- ElseIf DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) >= StartDate And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) <= EndDate Then
- If DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) >= .DataBodyRange(i, 2).Value And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) <= .DataBodyRange(i, 3).Value Then
- Tally = Tally - 1
- End If
- End If
- End If
- Next i
- End With
- HoldayTaken = Tally
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement