Advertisement
jdelano

Untitled

Jul 5th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dim holidaysArray(8) As Date
  2.  
  3. Private Sub btnCalculate_Click()
  4.  
  5.     ' loop the raw payroll data, calc hours for the week ending on
  6.    ' and including Sunday
  7.    
  8.     Dim totalWeekHours As Double
  9.    
  10.     ' you need to use long because excel can have data rows that exceed the max integer value
  11.    Dim lastRow As Long
  12.     Dim sheetRow As Long
  13.    
  14.     ' list of holidays to check against during the weekly hours calc
  15.    holidaysArray(0) = CDate("1/1/2025")
  16.     holidaysArray(1) = CDate("2/18/2025")
  17.     holidaysArray(2) = CDate("5/26/2025")
  18.     holidaysArray(3) = CDate("7/4/2025")
  19.     holidaysArray(4) = CDate("11/27/2025")
  20.     holidaysArray(5) = CDate("11/28/2025")
  21.     holidaysArray(6) = CDate("12/25/2025")
  22.     holidaysArray(7) = CDate("12/26/2025")
  23.    
  24.     ' this gets the last row that contains data in the sheet
  25.    lastRow = Sheet1.Cells(Sheet1.Rows.Count, "F").End(xlUp).Row
  26.  
  27.     ' loop through the data, sum hours for sunday, then add a blank row in the data
  28.    ' for formatting
  29.    totalWeekHours = 0
  30.     sheetRow = 2
  31.     Do Until sheetRow = lastRow + 1
  32.         If Weekday(Sheet1.Cells(sheetRow, "G").Value) = vbSunday Then
  33.             ' include Sundays hours
  34.            totalWeekHours = totalWeekHours + Sheet1.Cells(sheetRow, "K").Value
  35.            
  36.             ' add the total to the sheet and insert a line after it
  37.            Sheet1.Cells(sheetRow, "D").Value = totalWeekHours
  38.             Sheet1.Range("A" + CStr(sheetRow + 1)).EntireRow.Insert
  39.            
  40.             ' add a row to the last row given it has been pushed down the sheet
  41.            lastRow = lastRow + 1
  42.            
  43.             ' add to sheetrow to skip the new blank line added to the sheet
  44.            sheetRow = sheetRow + 1
  45.            
  46.             totalWeekHours = 0  ' reset to calc the next week
  47.        Else
  48.             ' add the hours worked minus the break to the total hours for the current week being totalled
  49.            totalWeekHours = totalWeekHours + Sheet1.Cells(sheetRow, "K").Value
  50.            
  51.             If IsHoliday(Sheet1.Cells(sheetRow, "G").Value) Then
  52.                 ' the date on this row matches a holiday, add a row below it, insert "Holiday" and add 8 hours
  53.                Sheet1.Range("A" + CStr(sheetRow + 1)).EntireRow.Insert
  54.                 sheetRow = sheetRow + 1
  55.                
  56.                 Sheet1.Cells(sheetRow, "G").Value = "Holiday"
  57.                 Sheet1.Cells(sheetRow, "K").Value = 8
  58.                
  59.                 ' a blank row is required after a holiday as well
  60.                Sheet1.Range("A" + CStr(sheetRow + 1)).EntireRow.Insert
  61.                
  62.                 ' looks like the holiday hours are not included in the weeks total hours
  63.                ' given the manual total in the example is 37.5 and not 45.5 on the last week
  64.                'totalWeekHours = totalWeekHours + 8
  65.                                
  66.                 ' add a row to the last row given it has been pushed down the sheet
  67.                lastRow = lastRow + 2
  68.                 sheetRow = sheetRow + 1  ' jump to the next line to calculate
  69.            End If
  70.         End If
  71.        
  72.         sheetRow = sheetRow + 1
  73.     Loop
  74.  
  75.     ' check if there is still a total hours, if so write it to the sheet for the last week
  76.    If totalWeekHours > 0 Then
  77.         Sheet1.Cells(sheetRow - 1, "D").Value = totalWeekHours
  78.     End If
  79.  
  80. End Sub
  81.  
  82. Private Function IsHoliday(dateToCheck As Date) As Boolean
  83.  
  84.     ' is the date in the holiday list?
  85.    Dim holidayArrayIndex As Integer
  86.    
  87.     ' check the array for the existance of the date passed (probably better to do with a dictionary
  88.    ' but with such a small list why add the overhead)
  89.    For holidayArrayIndex = 0 To 8
  90.         If dateToCheck = holidaysArray(holidayArrayIndex) Then
  91.             ' the date passed is a holiday
  92.            IsHoliday = True
  93.             Exit Function
  94.         End If
  95.     Next holidayArrayIndex
  96.    
  97.     ' if it gets here then the date isn't a holiday
  98.    IsHoliday = False
  99.    
  100. End Function
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement