Track opening time and closing time ofworkbook

EinarOSies

Board Regular
Joined
Feb 15, 2021
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
Please I may want a code to calculate the time I used in working with a workbook. The details are when I open the workbook it stores it in a cell then when closing it stores it in another cell then calculate the difference between the my opening time and closing time and present it to me with a message box of the difference or the amount of time I used working with the workbook. Please thanks
 
Yes I got a way around it too by removing the Audit xlVeryHidden line and changed the name of the worksheet into a new name that is 1Audit and it worked
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This is a followup question since columns C and D get populated by the Open Time and Close Time respectively I created an addition column G to calculate the difference.
Please can I get a line of code that displays a message box whiles closing the workbook the difference of column G whiles the Close Time and Open WB Name get populated.
Thanks,Einar
 
Upvote 0
Okay thanks seems I got a way around it by Audit xlVeryHidden and renaming the worksheet to 1Audit.
 
Upvote 0
Here is the code that display msg telling Elapse time before closing workbook. Note that I added in

Private Declaration
Private Const ELAPSE_TIME_COL = 7

Workbook Close Section
Dim sTime As Date
Dim eTime As Date
Dim strMin As String
Dim strSec As String
......
sTime = .Cells(RowNum, OPEN_TIME_COL)
eTime = .Cells(RowNum, CLOSE_TIME_COL)
strMin = DateDiff("n", sTime, eTime)
strSec = DateDiff("s", sTime, eTime) - strMin
.Cells(RowNum, ELAPSE_TIME_COL) = strMin & " min " & strSec & " sec"
......
MsgBox "Elapse Time = " & WS.Cells(RowNum, ELAPSE_TIME_COL)
'ActiveWorkbook.Save

If you do not want msg asking whether to save ot not, then unremark the ActiveWorkbook.Save

I think if someone cancel not to save then the closing time will be blank in Audit sheet.

VBA Code:
''''''''''''''''''''''''''''''''''''''''''
' WORKBOOK USAGE TRACKING
''''''''''''''''''''''''''''''''''''''''''
Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( _
    ByVal lpBuffer As String, _
    nSize As Long) As Long
Private Declare PtrSafe Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" ( _
    ByVal lpBuffer As String, _
    nSize As Long) As Long

Private pAuditSheet As Worksheet
Private Const USERNAME_COL = 1
Private Const COMPUTERNAME_COL = 2
Private Const OPEN_TIME_COL = 3
Private Const CLOSE_TIME_COL = 4
Private Const OPEN_WB_NAME_COL = 5
Private Const CLOSE_WB_NAME_COL = 6
Private Const ELAPSE_TIME_COL = 7
Private Const KEEP_ONLY_LAST_N_ENTRIES = 10

Private Sub Workbook_Open()
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Workbook_Open
    ' Runs when the workbook is opened.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim WS As Worksheet
    Dim RowNum As Long
    Dim N As Long
    Dim S As String
    
    Application.ScreenUpdating = False
    On Error Resume Next
    Err.Clear
    Set WS = Me.Worksheets("Audit")
    If Err.Number = 9 Then
        Set WS = Sheets.Add(Before:=Sheets(1))
        WS.Name = "Audit"
    End If
    On Error GoTo 0
    With WS
        If .Cells(1, USERNAME_COL).Value = vbNullString Then
            .Cells(1, USERNAME_COL).Value = "User Name"
            .Cells(1, COMPUTERNAME_COL).Value = "Computer Name"
            .Cells(1, OPEN_TIME_COL).Value = "Open Time"
            .Cells(1, CLOSE_TIME_COL).Value = "Close Time"
            .Cells(1, ELAPSE_TIME_COL).Value = "Elapse Time"
            .Cells(1, OPEN_WB_NAME_COL).Value = "Open WB Name"
            .Cells(1, CLOSE_WB_NAME_COL).Value = "Close WB Name"
        End If
        .Visible = xlSheetVeryHidden
        RowNum = .Cells(.Rows.Count, USERNAME_COL).End(xlUp)(2, 1).Row
        N = 255
        S = String(N, vbNullChar)
        N = GetUserName(S, N)
        .Cells(RowNum, USERNAME_COL).Value = TrimToNull(S)
        N = 255
        S = String(N, vbNullChar)
        N = GetComputerName(S, N)
        .Cells(RowNum, COMPUTERNAME_COL).Value = TrimToNull(S)
        .Cells(RowNum, OPEN_TIME_COL).Value = Now
        ' Leave Close Time empty. It will be filled on close.
        .Cells(RowNum, CLOSE_TIME_COL).Value = vbNullString
        .Cells(RowNum, OPEN_WB_NAME_COL).Value = ThisWorkbook.FullName
        ' Leave Close Name empty. It will be filled on close.
        .Cells(RowNum, CLOSE_WB_NAME_COL).Value = vbNullString
        .UsedRange.Columns.AutoFit
    End With
    Application.ScreenUpdating = True
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Workbook_BeforeClose
' Runs when the workbook is closed.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim WS As Worksheet
    Dim RowNum As Long
    Dim EndRow As Long
    Dim LastDel As Long
    Dim FirstDel As Long
    Dim sTime As Date
    Dim eTime As Date
    Dim strMin As String
    Dim strSec As String
    
    Application.ScreenUpdating = False
    Set WS = Worksheets("Audit")
    With WS
        RowNum = .Cells(.Rows.Count, CLOSE_TIME_COL).End(xlUp).Row + 1
        .Cells(RowNum, CLOSE_TIME_COL).Value = Now
        .Cells(RowNum, CLOSE_WB_NAME_COL).Value = ThisWorkbook.FullName
        .UsedRange.Columns.AutoFit
        If KEEP_ONLY_LAST_N_ENTRIES > 0 Then
            EndRow = .Cells(.Rows.Count, USERNAME_COL).End(xlUp).Row
            If EndRow > 2 Then
                FirstDel = 2
                LastDel = EndRow - KEEP_ONLY_LAST_N_ENTRIES
                If LastDel > 2 Then
                    .Cells(FirstDel, "A").Resize(LastDel - 1, 1).Select
                End If
            End If
        End If
        sTime = .Cells(RowNum, OPEN_TIME_COL)
        eTime = .Cells(RowNum, CLOSE_TIME_COL)
        strMin = DateDiff("n", sTime, eTime)
        strSec = DateDiff("s", sTime, eTime) - strMin
        .Cells(RowNum, ELAPSE_TIME_COL) = strMin & " min " & strSec & " sec"
    End With
    Application.ScreenUpdating = True
    MsgBox "Elapse Time = " & WS.Cells(RowNum, ELAPSE_TIME_COL)
    'ActiveWorkbook.Save
    
End Sub


Private Function TrimToNull(S As String) As String
'''''''''''''''''''''''''''''''''''''''''''''''''''
' TrimToNull
' Returns the portion of string S that is to the
' left of the vbNullChar, Chr(0).
'''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    N = InStr(1, S, vbNullChar)
    If N = 0 Then
        TrimToNull = S
    Else
        TrimToNull = Left(S, N - 1)
    End If
End Function
''''''''''''''''''''''''''''''''''''''''''
' END CODE
''''''''''''''''''''''''''''''''''''''''''
 
Upvote 0
Solution
Sorry. I screwed up the time calculation. Replace this line, I hope it is correct now.

strMin = Int(DateDiff("s", sTime, eTime) / 60)
strSec = DateDiff("s", sTime, eTime) - strMin * 60
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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