Log users activity - open & close

Steven101

Board Regular
Joined
Oct 27, 2014
Messages
62
Hi, I currently have a workbook that when opened updates a Notepad with the users name, date & time opened. using the below code...

Code:
Private Sub Workbook_Open()
Sheets("Select Handler").Select
Open "myfilelcation.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Application.UserName, Now
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub

The log file is not vey user friendly and was hoping to save an excel workbook rather than a notepad in the following format;

Column A Username
Column B Date opened
Column C Time opened
Column D Date Closed
Column E Time Closed

Any help would be appreciated.
 

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.
Try this code,
you Will have to change name of the log sheet, and it should work

Code:
Private Sub Workbook_Open()Dim lastrow As Integer
Dim user As String


user = Environ$("username")
    lastrow = Sheets("Log").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Log").Range("A" & lastrow + 1).Value = user
Sheets("Log").Range("B" & lastrow + 1).Value = Format(Now(), "yyyy.mm.dd")
Sheets("Log").Range("C" & lastrow + 1).Value = Format(Now(), "hh:mm")
End Sub




Private Sub Workbook_BeforeClose(Cancel As Boolean)


lastrow = Sheets("Log").Range("D" & Rows.Count).End(xlUp).Row
Sheets("Log").Range("D" & lastrow + 1).Value = Format(Now(), "yyyy.mm.dd")
Sheets("Log").Range("E" & lastrow + 1).Value = Format(Now(), "hh:mm")
End Sub
 
Upvote 0
Thank for the response... I have amended slightly to get what I was looking for. My issue is I ant the Log Sheet (workbook) to be different workbook.

Say: C:\Users\Desktop\Log Test Input.xlsm

Code:
Private Sub Workbook_Open()

Dim lastrow As Integer
Dim user As String

user = Application.UserName
lastrow = Sheets("Log").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Log").Range("A" & lastrow + 1).Value = user
Sheets("Log").Range("B" & lastrow + 1).Value = Format(Now(), "dd/mm/yyyy")
Sheets("Log").Range("C" & lastrow + 1).Value = Format(Now(), "hh:mm:ss")
Sheets("Log").Range("D" & lastrow + 1).Value = "Open"
End Sub



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim lastrow As Integer
Dim user As String
user = Application.UserName
lastrow = Sheets("Log").Range("D" & Rows.Count).End(xlUp).Row

Location.Sheets("Log").Range("A" & lastrow + 1).Value = user
Location.Sheets("Log").Range("B" & lastrow + 1).Value = Format(Now(), "dd/mm/yyyy")
Location.Sheets("Log").Range("C" & lastrow + 1).Value = Format(Now(), "hh:mm:ss")
Location.Sheets("Log").Range("D" & lastrow + 1).Value = "Closed"
End Sub


Thanks
 
Upvote 0
You may want to keep a flat file for speed of appending. That file can be a .csv which will open in Excel with delimited columns.
Code:
Private Sub Workbook_Open()
Sheets("Select Handler").Select
vLog = Application.UserName & "," & Format$(Now, "m/d/yyyy") & "," & Format(Now, "hh:mm") & ",Open"
Open "C:\[COLOR=#574123]Users\Desktop\Log Test Input.[/COLOR]csv" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , vLog
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
vLog = Application.UserName & "," & Format$(Now, "m/d/yyyy") & "," & Format(Now, "hh:mm") & ",Close"
Open "C:\[COLOR=#574123]Users\Desktop\Log Test Input.[/COLOR]csv" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , vLog
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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