Advertisement
Guest User

vbscript custom excel function

a guest
Feb 21st, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function HoldayTaken(Employee As String, StartDate As Date, EndDate As Date) As Integer
  2.     Application.Volatile True
  3.     Dim ExclDates
  4.     'We Grab the year from our records to keep it current
  5.    NewYearDay = "01/01"
  6.     Christmas = "25/12"
  7.    
  8.     Dim tbl As ListObject
  9.     Set tbl = Sheets("DataSheet").ListObjects("Holidays")
  10.     Dim Enabled_ As Boolean
  11.     Enabled_ = True
  12.     With tbl
  13.         For i = 1 To .ListRows.Count
  14.             If .DataBodyRange(i, 1).Value = Employee Then
  15.                 'If Holday StartDate is within Range
  16.                If .DataBodyRange(i, 2).Value >= StartDate And .DataBodyRange(i, 2).Value <= EndDate Then
  17.                     'If Holiday EndDate is within Range
  18.                    If .DataBodyRange(i, 3).Value >= StartDate And .DataBodyRange(i, 3).Value <= EndDate Then
  19.                         'Return Days
  20.                        Tally = Tally + DateDiff("d", .DataBodyRange(i, 2).Value, .DataBodyRange(i, 3).Value)
  21.                     'If Holiday EndDate is out of range
  22.                    ElseIf .DataBodyRange(i, 3).Value >= EndDate Then
  23.                         Tally = Tally + DateDiff("d", .DataBodyRange(i, 2).Value, EndDate)
  24.                     End If
  25.                    
  26.                     'Apply 1 to date
  27.                    If Enabled_ Then
  28.                         Tally = Tally + 1
  29.                         Enabled_ = False
  30.                     End If
  31.                    
  32.                 'If Holiday covers the entire week
  33.                ElseIf .DataBodyRange(i, 3).Value < StartDate And .DataBodyRange(i, 3).Value > EndDate Then
  34.                     Tally = Tally + DateDiff("d", StartDate, EndDate)
  35.                    
  36.                     'Apply 1 to date
  37.                    If Enabled_ Then
  38.                         Tally = Tally + 1
  39.                         Enabled_ = False
  40.                     End If
  41.                    
  42.                 'If Holiday StartDate is less than Week StartDate
  43.                ElseIf .DataBodyRange(i, 2).Value < StartDate Then
  44.                     If .DataBodyRange(i, 3).Value >= StartDate And .DataBodyRange(i, 3).Value <= EndDate Then
  45.                         Tally = Tally + DateDiff("d", StartDate, .DataBodyRange(i, 3).Value)
  46.                    
  47.                         'Apply 1 to date
  48.                        If Enabled_ Then
  49.                             Tally = Tally + 1
  50.                             Enabled_ = False
  51.                         End If
  52.                        
  53.                     Else
  54.                         Tally = Tally + 0
  55.                     End If
  56.                 Else
  57.                     Tally = Tally + 0
  58.                 End If
  59.          
  60.                 If DateValue(Christmas & "/" & Year(.DataBodyRange(i, 2).Value)) >= StartDate And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 2).Value)) <= EndDate Then
  61.                     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
  62.                         Tally = Tally - 1
  63.                     End If
  64.                 ElseIf DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) >= StartDate And DateValue(Christmas & "/" & Year(.DataBodyRange(i, 3).Value)) <= EndDate Then
  65.                     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
  66.                         Tally = Tally - 1
  67.                     End If
  68.                 End If
  69.                
  70.                 If DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) >= StartDate And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 2).Value)) <= EndDate Then
  71.                     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
  72.                         Tally = Tally - 1
  73.                     End If
  74.                 ElseIf DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) >= StartDate And DateValue(NewYearDay & "/" & Year(.DataBodyRange(i, 3).Value)) <= EndDate Then
  75.                     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
  76.                         Tally = Tally - 1
  77.                     End If
  78.                 End If
  79.          
  80.             End If
  81.         Next i
  82.     End With
  83.    
  84.     HoldayTaken = Tally
  85.    
  86. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement