vba: How long workbook was active

samahiji

Board Regular
Joined
Oct 6, 2015
Messages
82
Office Version
  1. 2019
Platform
  1. Windows
Hi
I'm looking for a vba code to monitor the time spent since the workbook was opened.
I want to track the time from opening the sheet till it was closed and create a log for such event.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
1683182443192.png

Place code in "ThisWorkbook

VBA Code:
Private Sub Workbook_Open()
  Dim nLastUsedRowIndex As Integer
 
  nLastUsedRowIndex = Application.WorksheetFunction.CountA(Worksheets("Access Time").Range("A:A")) + 1
  Range("A" & nLastUsedRowIndex) = Now
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Dim nLastUsedRowIndex As Integer
 
  nLastUsedRowIndex = Application.WorksheetFunction.CountA(Worksheets("Access Time").Range("B:B")) + 1
  Range("B" & nLastUsedRowIndex) = Now

  'Save the file and then exit the application
  ActiveWorkbook.Save
  Application.Quit
End Sub

This is for a worksheet called "Access Time".
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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