Edit Existing VBA to subtract .5 for lunch each if total hours are 6.5 or greater each day

cal5859

New Member
Joined
Apr 20, 2018
Messages
1
We purchased a time clock, and the time clock company supplied us with a macro that total the daily hours as well as the total hours for any date range that we choose. Our company policy is if an employee works 6.5 or more hours per day, we subtract .5 hours from that days hours for lunch. I would like to edit the code to perform this action while calculating the hours.
HERE IS A COPY OF THE TIME CLOCK PUNCH REPORT THAT GENERATES FroM OUR TIME CLOCK
Time Card Report
10/1/2015 - 10/30/2015
Downloaded:10/27/2015 02:34 PM
No: 496386Name: NAMEDept:Shift: NULLDaily totals
DateWeek(IN)(OUT)(IN)(OUT)(IN)(OUT)(IN)(OUT)Regular HoursOT HoursOther HoursNotes
10/01THU
10/02FRI
10/03SAT
10/04SUN
10/05MON
10/06TUE
10/07WED
10/08THU
10/09FRI
10/10SAT5:5514:58
10/11SUN5:5814:58
10/12MON5:5714:49
10/13TUE0:000:00
10/14WED14:2421:01
10/15THU14:1520:15
10/16FRI0:000:00
10/17SAT0:000:00
10/18SUN0:000:00
10/19MON0:000:00
10/20TUE0:000:00
10/21WED14:2521:51
10/22THU14:1821:03
10/23FRI0:000:00
10/24SAT6:0015:03
10/25SUN6:0314:59
10/26MON0:000:00
10/27TUE0:000:00
10/28WED0:000:00
10/29THU0:000:00
10/30FRI0:000:00
TOTAL:
Work Total (hrs):Overtime (hrs)Other (hrs):Total (hrs):
Employee Signature:Manager Signature:Date:
No: 490794Name: NAMEDept:Shift: NULLDaily totals
DateWeek(IN)(OUT)(IN)(OUT)(IN)(OUT)(IN)(OUT)Regular HoursOT HoursOther HoursNotes
10/01THU
10/02FRI
10/03SAT
10/04SUN
10/05MON
10/06TUE
10/07WED
10/08THU14:18
10/09FRI6:2717:14
10/10SAT0:000:00
10/11SUN0:000:00
10/12MON6:3017:34
10/13TUE6:1919:09
10/14WED9:5511:30
10/15THU0:000:00
10/16FRI0:000:00
10/17SAT0:000:00
10/18SUN0:000:00
10/19MON6:2617:31
10/20TUE6:3317:07
10/21WED0:000:00
10/22THU6:2818:33
10/23FRI6:2517:32
10/24SAT0:000:00
10/25SUN0:000:00
10/26MON6:3118:18
10/27TUE6:24
10/28WED0:00
10/29THU0:00
10/30FRI0:00
TOTAL:
Work Total (hrs):Overtime (hrs)Other (hrs):Total (hrs):
Employee Signature:Manager Signature:Date:

<tbody>
</tbody>

HERE IS THE VBA CODE TO CALCULATE THE HOURS


Code:
' BioTouch Totals


Const FirstPunchColumn = 3
Const cTotalsColumn = 11


Sub CalculateTimeCards()
    formGetFormat.Show
End Sub


Sub CalcTimeCards(bHundredths As Boolean)
    'Application.ScreenUpdating = False
    
    Dim nRow As Integer
    Dim dtIn As Date
    Dim dtOut As Date
    Dim dDayTotal As Double
    Dim dPeriodTotal As Double
    Dim bOut As Boolean


    On Error GoTo ErrorHandler
    
    BlankCount = 0
    nRow = 4
    With Worksheets("Sheet1")
        Do While BlankCount < 10
            If .Cells(nRow, 1) = "" Then
                BlankCount = BlankCount + 1
            Else
                If Left(.Cells(nRow, 1), 3) = "No:" Then
                    BlankCount = 0
                    dPeriodTotal = 0
                    ' Get the punch data
                    nRow = nRow + 2
                    
                    Do While Trim(.Cells(nRow, 1)) <> ""
                        nCol = FirstPunchColumn
                        dDayTotal = 0
                        bOut = False
                        If Trim(.Cells(nRow, nCol)) = "" Then
                            .Cells(nRow, cTotalsColumn) = ""
                        Else
                            ' Iterate the timecard columns
                            Do While Trim(.Cells(nRow, nCol)) <> ""
                                ' Get the punch
                                If Not bOut Then
                                    dtIn = .Cells(nRow, nCol)
                                    bOut = True
                                Else
                                    dtOut = .Cells(nRow, nCol)
                                    dDayTotal = dDayTotal + (dtOut - dtIn)
                                    If dtOut < dtIn Then
                                        ' Crossed midnight. Add a day
                                        dDayTotal = dDayTotal + 1
                                    End If
                                    bOut = False
                                End If
                                nCol = nCol + 1
                            Loop
                            If Not bOut Then
                                If bHundredths Then
                                    .Cells(nRow, cTotalsColumn).NumberFormat = "0.00"
                                    .Cells(nRow, cTotalsColumn) = 24 * dDayTotal
                                Else
                                    .Cells(nRow, cTotalsColumn).NumberFormat = "[h]:mm"
                                    .Cells(nRow, cTotalsColumn) = HoursAndMins(dDayTotal)
                                End If
                                dPeriodTotal = dPeriodTotal + dDayTotal
                            Else
                                .Cells(nRow, cTotalsColumn) = "MP"
                            End If
                        End If
                        nRow = nRow + 1
                    Loop
                    .Cells(nRow, cTotalsColumn).HorizontalAlignment = xlCenter
                    If bHundredths Then
                        .Cells(nRow, cTotalsColumn).NumberFormat = "0.00"
                        .Cells(nRow, cTotalsColumn) = 24 * dPeriodTotal
                    Else
                        .Cells(nRow, cTotalsColumn).NumberFormat = "[h]:mm"
                        .Cells(nRow, cTotalsColumn) = HoursAndMins(dPeriodTotal)
                    End If
                End If
            End If
            nRow = nRow + 1
        Loop
    End With
    Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
    MsgBox "Problem accessing the Time Card Spreadsheet. Make sure that the time cards are opened in the same " & _
        "Excel instance as this macro", vbOKOnly, "Error"
Exit Sub
End Sub




Function HoursAndMins(ByVal d As Double) As String
    Dim Hours As Integer
    Dim Min As Integer
    Dim str1 As String
        
    d = d * 24
    Hours = Int(d)
    Min = Round((d - Hours) * 60, 0)
    If Len(Min) < 2 Then
        HoursAndMins = Hours & ":0" & Min
    Else
        HoursAndMins = Hours & ":" & Min
    End If
End Function
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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