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 :
- 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).
- Workbook is re-opened before logging-out (could use a Windows Automated Task for that, i think), then again manually saved & closed.
- The worksheet in which you are working is called "Template"
- 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.