Fix timesheet totals for regular time and overtime, Maybe Macro

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,129
Office Version
  1. 365
Platform
  1. Windows
Hi if anyone can help.

I update the sheets below because, If a guy works over 40 hours he gets overtime regardless if he works 10 hours in one day. You can see on the first sheet column Y is adding up to 48. Since its over 40 i have to break it down as you can see on second sheet. You can see 8/2 then 6 stays the same because its under 8. Then 8/4 for Wednesday then 8/2 for Thursday then Friday stays the same because the Y Column total reached 40. So Friday he doesn't get 8/2. Friday stays at 10.
The second guy only worked 39 hours this week. Since its under 40 nothing happens. I would have this time down the whole sheet. Trying to See maybe a Macro would be good for this or a formula but I wouldn't really know where would the formula go on the Sheet. Why was thinking a macro. The columns with numbers that would change only are F,G I,J L,M O,P R,S U,V W,X . Ignore all DT columns. They are never used.

Sample Sheet

Timesheet Test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
5MondayTuesdayWednesdayThursdayFridaySaturdaySundayWEEKLY TOTALS
62/272/283/13/23/33/43/5
7No.NTIDJSTOTDTSTOTDTSTOTDTSTOTDTSTOTDTSTOTSTOTSTOTDT
81Bob7 Super10025310.06.012.010.010.048.0-
92Jason17 Super1908969.09.05.013.03.039.0-
Timesheet
Cells with Data Validation
CellAllowCriteria
C8:C9List=$AH$844:$AH$944


Results

Timesheet Test 2.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
5MondayTuesdayWednesdayThursdayFridaySaturdaySundayWEEKLY TOTALS
62/272/283/13/23/33/43/5
7No.NTIDJSTOTDTSTOTDTSTOTDTSTOTDTSTOTDTSTOTSTOTSTOTDT
81Bob7 Super52538.02.06.08.04.08.02.010.040.08.0
92Jason17 Super98969.09.05.013.03.039.0-
Timesheet
Cells with Data Validation
CellAllowCriteria
C8:C9List=$AH$844:$AH$944
 
Slightly different format, but it can be done with formulas.
I added a row for ST and OT hours. Top row is total hours for the day
2nd row splits based on total hours

TimesheetOTHours.xlsx
ABCDEFGHIJKLMNOPQRSTU
1date2/27/2023MondayTuesdayWednesdayThursdayFridaySaturdaySundayWEEKLY TOTALS
22/272/283/13/23/33/43/5
3
4
5No.NTIDJSTOTSTOTSTOTSTOTSTOTSTOTSTOTSTOT
61Bob7 Super100253106121010408
748826 8482820 0 
8-
92Jason17 Super19089699513339
10399 9 5 13 3 0 0 
11-
Sheet1
Cell Formulas
RangeFormula
F2F2=$B1+0
H2H2=$B1+1
J2J2=$B1+2
L2L2=$B1+3
N2N2=$B1+4
P2P2=$B1+5
R2R2=$B1+6
T6,T9T6=IF(SUM(F6:S6)>40,40,SUM(F6:S6))
U6U6=IF(SUM(F6:S6)>40,SUM(F6:S6)-T6,"")
F7,R10,P10,N10,L10,J10,H10,F10,R7,P7,N7,L7,J7,H7F7=IF(($T6+$U6)>40,IF(F6>8,8,F6),F6)
G7,S10,Q10,O10,M10,K10,I10,G10,O7,M7,K7,I7G7=IF(F7>8,"",IF(AND($T6+$U6>40,F6>8),F6-8,""))
Q7,S7Q7=IF(AND($T6+$U6>40,P6>8),P6-8,"")
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I a sample sheet for you. every week is different more range less range going downward. How would i post the sheet Xl2bb or another way?
xl2bb. Or do have a google email address? You could upload the file to google and post the link to the file here.
 
Upvote 0
Your sheet could be much more efficient.

What is the full information on how OT must be calculated for your business and jurisdiction?
What is the purpose of allocating the OT to particular days?
Do you do any analysis of the time by month or Year?
 
Upvote 0
Hi, I need to use my sheet because i have made it work with everything. Since there is not Overtime unless you go over 40 hours. I have the guys put in the total hours each day the guys work thats in the ST column and then i break it down on my end. Or else they would be putting time in during the week. Like 6 /2 or 8 /4. If the guy didnt work that week 40 hours i would have to change both back to 8 and 12. not 6 / 2 8 / 4 . I really think i need a macro to go through each line and make the changes i need if the week is over 40 hours. If its under in column Y then nothing happens on the Line.
 
Upvote 0
Hi, I need to use my sheet because i have made it work with everything. Since there is not Overtime unless you go over 40 hours. I have the guys put in the total hours each day the guys work thats in the ST column and then i break it down on my end. Or else they would be putting time in during the week. Like 6 /2 or 8 /4. If the guy didnt work that week 40 hours i would have to change both back to 8 and 12. not 6 / 2 8 / 4 . I really think i need a macro to go through each line and make the changes i need if the week is over 40 hours. If its under in column Y then nothing happens on the Line.
So, do your workers have their own Worksheets where they enter their hours. Or are you both using (entering data on) the same Worksheet(s)?
 
Upvote 0
Everyone is using the same worksheet. Once filled in its sent back to me to break it down.
 
Upvote 0
I added a button (HOURS BREAKDOWN) to the sample modified timesheet. The button hides/unhides the ST and OT row (addition that I suggested). The workers would enter their daily hours on Row 6, Row 9, ... for example). When you do the ST and OT breakdown of hours at the end of the week, you would click the HOURS BREAKDOWN button to display the second row so that you could do the ST and OT analysis. Hopefully, with the functions that I added to this worksheet, this breakdown would be automatic.

In an actual working version of this Worksheet you would probably want to add a level of security for toggling the display of the ST/OT hours.
Unfortunately, the XL2BB add-in does not capture controls added to a Worksheet. Here is an image of what it currently looks like.
1691072782550.png


Here's a mini-sheet with breakdown hours hidden and displayed.

Hidden ST and OT hours (note that rows 7 and 10 are not being displayed)
TimesheetOTHours.xlsx
ABCDEFGHIJKLMNOPQRSTU
1date2/27/2023MondayTuesdayWednesdayThursdayFridaySaturdaySundayWEEKLY TOTALS
22/272/283/13/23/33/43/5
3
4DAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURS
5No.NTIDJSTOTSTOTSTOTSTOTSTOTSTOTSTOTSTOT
61Bob7 Super100253106121010408
8-
92Jason17 Super190896995135401
Sheet1
Cell Formulas
RangeFormula
F2F2=$B1+0
H2H2=$B1+1
J2J2=$B1+2
L2L2=$B1+3
N2N2=$B1+4
P2P2=$B1+5
R2R2=$B1+6
T6,T9T6=IF(SUM(F6:S6)>40,40,SUM(F6:S6))
U6,U9U6=IF(SUM(F6:S6)>40,SUM(F6:S6)-T6,"")


Button toggled to display ST/OT hours breakdown.
TimesheetOTHours.xlsx
ABCDEFGHIJKLMNOPQRSTU
1date2/27/2023MondayTuesdayWednesdayThursdayFridaySaturdaySundayWEEKLY TOTALS
22/272/283/13/23/33/43/5
3
4DAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURSDAILY HOURS
5No.NTIDJSTOTSTOTSTOTSTOTSTOTSTOTSTOTSTOT
61Bob7 Super100253106121010408
748826 848210 0 0 
8-
92Jason17 Super190896995135401
Sheet1
Cell Formulas
RangeFormula
F2F2=$B1+0
H2H2=$B1+1
J2J2=$B1+2
L2L2=$B1+3
N2N2=$B1+4
P2P2=$B1+5
R2R2=$B1+6
T6,T9T6=IF(SUM(F6:S6)>40,40,SUM(F6:S6))
U6,U9U6=IF(SUM(F6:S6)>40,SUM(F6:S6)-T6,"")
F7,R7,P7F7=IF($A7>40,IF(F6>8,8,F6),F6)
G7G7=IF(F7>8,"",IF(AND(AND($A7>40,F6>8),(F6-F7)<=$U6),F6-8,""))
H7,N7,L7,J7H7=IF($A7>40,IF(AND(SUM($F6:H6)<=40,H6>8),8,H6),H6)
I7I7=IF(H7>8,"",IF(AND(AND($A7>40,H6>8),SUM((H6-H7),G7)<=$U6),H6-8,""))
K7K7=IF(J7>8,"",IF(AND(AND($A7>40,J6>8),SUM((J6-J7),G7,I7)<=$U6),J6-8,""))
M7M7=IF(L7>8,"",IF(AND(AND($A7>40,L6>8),SUM((L6-L7),G7,I7,K7)<=$U6),L6-8,""))
O7,Q7O7=IF(N7>8,"",IF(AND(AND($A7>40,N6>8),SUM((N6-N7),G7,I7,K7,M7)<=$U6),N6-8,""))
S7S7=IF(R7>8,"",IF(AND(AND($A7>40,R6>8),SUM((R6-R7),G7,I16,K7,M7,O7,Q7)<=$U6),R6-8,""))
 
Upvote 0
I a sample sheet for you. every week is different more range less range going downward. How would i post the sheet Xl2bb or another way?
to post a sample or mini-sheet you will have to add the XL2BB add-in to Excel. Here is the link to with instructions and the download for doing that:
XL2BB

Also, here is a VBA macro that splits ST and OT hours on a worksheet. However, it will probably need some changes.
1) Would you want to do the ST/OT split on a line-by-line basis (i.e. run the macro for each row in a sheet). Or run the macro (current setup) on an either sheet of post work hours?
2) Right now if the Total OT hours in Column Z is greater than 0 the macro assumes that ST and OT hours have already been calculated for that row and does not attempt to recalculate them.
It does generate a message to inform you which row(s) had a > 0 value in Total OT hours.

Try this. Let me know if changes are needed.

VBA Code:
Option Explicit

Public Sub AdjustSTAndOTHours(Optional sheet As Worksheet)
  Dim rng As Range
  Dim rngTotals As Range
  Dim lr As Long
  Dim begRow As Long
  Dim TotalHours, TotalSTHours, TotalOTHours As Integer
  Dim DailyHours As Integer
  Dim STHours As Integer
  Dim OTHours As Integer
  Dim othoursdiff As Integer
  Dim SumOTHours As Integer
  Dim dy, c, r As Integer
  Dim tmp
  Dim msgstr As String

  If sheet Is Nothing Then
    Set sheet = ActiveSheet
  End If
  lr = sheet.Range("A" & Rows.Count).End(xlUp).Row - 1
  
  begRow = WorksheetFunction.Match("No.", sheet.Range("A:A"), 0) + 1
  
  TotalSTHours = 0
  TotalOTHours = 0
  msgstr = ""
  For r = begRow To lr
    Set rng = sheet.Range("F" & r & ":X" & r)
    Set rngTotals = sheet.Range("Y" & r & ":Z" & r)
'    For c = 1 To 15 Step 3
'      Debug.Print rng(1, c), rng(1, c + 1)
'    Next c
    TotalHours = WorksheetFunction.SumIf(rng, ">0")
    'Are ST and OT hours already done?
    If rngTotals(1, 2) > 0 Then 'it appears OT hours are already calculated
      msgstr = msgstr & ", " & r
    Else
      If TotalHours > 40 Then
        TotalSTHours = 40
        TotalOTHours = TotalHours - 40
      Else
        TotalSTHours = TotalHours
        TotalOTHours = 0
      End If
      SumOTHours = 0
      If TotalHours > 40 Then
        For dy = 1 To 7 'monday thru sunday
          If dy < 6 Then
            c = (dy - 1) * 3 + 1
          Else
            c = 15 + (dy - 6) * 2 + 1
          End If
          If rng(1, c) > 8 And SumOTHours < TotalOTHours Then
            STHours = 8
            OTHours = rng(1, c) - 8
            SumOTHours = SumOTHours + OTHours
            If SumOTHours > TotalOTHours Then
              othoursdiff = (SumOTHours - TotalOTHours)
              STHours = STHours + othoursdiff
              OTHours = OTHours - othoursdiff
              SumOTHours = SumOTHours - othoursdiff
            End If
            rng(1, c) = STHours
            rng(1, c + 1) = OTHours
  '          Debug.Print STHours, OTHours, SumOTHours
          Else
            STHours = rng(1, c)
  '          Debug.Print STHours
          End If
        Next dy
      End If
      Set rng = sheet.Range("Y" & r & ":Z" & r)
  '    rng(1, 1) = TotalSTHours 'do not write these values there are formulas in the cells
  '    rng(1, 2) = TotalOTHours 'do not write these values there are formulas in the cells
      If TotalOTHours <> SumOTHours Then
        MsgBox "Row: " & r & "OT Hours mismatch.  Timesheet OT hours = " & SumOTHours & _
          ", Total OT Hours = " & TotalOTHours
      End If
    End If
    
  Next r
  If Len(msgstr) > 0 Then
    MsgBox "ST and OT hours already calculated for row(s): " & Mid(msgstr, 3)
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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