Question

I looking at building a template for managing my work hours.

currently i have been looking around for a few examples but haven't found to many that make much sense to me :(.

Would i am looking at is a excel template with a macro to record the First logon event in the morning and the final logoff(Or Lock) Event at the end of the day.

This would be need to be done for a week at a time but just need examples to get me started.

Something a long the lines of...:

If event log date the date is specify in a cell then return the first logon time then return the last logoff or lock event for the same date.

This would just be the basics, i would then fill in the rest to accounts for breaks etc...

Thank you.

Was it helpful?

Solution

I went ahead and made some assumptions regarding some of the functionality, as you will hopefully be able to adapt it to suit your own needs.

I assumed the following scenario would the most likely :

  1. Workbook is opened in the morning (place shortcut to workbook in start-up folder), then manually saved & closed (I let you have the fun of figuring out how to automatize this one).
  2. Workbook is re-opened before logging-out (could use a Windows Automated Task for that, i think), then again manually saved & closed.
  3. The worksheet in which you are working is called "Template"
  4. Suggested columns : Date | Log-On Time | Log-Out Time | Total Time

If you inadvertently open the workbook before really logging-out, simply erase the erroneous information, save, close, and re-open.

Finally, here's the code :

Private Sub Workbook_Open()
    Dim currentTime As Date, currentDay As Date
    currentTime = Format(Now(), "hh:mm")
    currentDay = Format(Now(), "mm/dd/yyyy")

    Dim template As Worksheet
    Set template = Worksheets("Template")

    With template
        'find the last log-out to use the next line
        last = .Cells(Rows.Count, 3).End(xlUp).Row + 1

        If .Cells(last, 2) = "" Then
        'log-on event
            .Cells(last, 1) = currentDay
            .Cells(last, 2) = currentTime

        Else
        'log-out event
            .Cells(last, 3) = currentTime
            .Cells(last, 4) = Format(currentTime - CDate(.Cells(last, 2)), "hh:mm")
        End If
    End With
End Sub

EDIT : I think you may also be able to figure out how to easily calculate break times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top