VBA to log times workbook has been opened into rows ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hi,
i want to be able to monitor what users have used the workbook and log the info into rows.
Date + Time is enough but is it also possible to get current windows user in excel ?


example - Sheet("Log")
27/12/2016 07:14 Bob
27/12/2016 09:47 Tim
27/12/2016 12:41 James

thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
done this, it works fine but maybe theres a better solution

Code:
Sub Macro3()
    Dim strUser As String
    strUser = Environ("UserName")
    
    Range("A1").FormulaR1C1 = "=NOW()"
    Range("B1").FormulaR1C1 = strUser
    Range("A1").Copy
    Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
 
Last edited:
Upvote 0
and better:

Code:
Private Sub Workbook_Open()
    Dim strUser As String
    strUser = Environ("UserName")
   Sheets("Log").Range("A1").FormulaR1C1 = "=NOW()"
    Sheets("Log").Range("B1").FormulaR1C1 = strUser
    Sheets("Log").Range("A1").Copy
    Sheets("Log").Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Log").Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
 
Upvote 0
Hi Jumbo, you can reduce your code further to:
Code:
Private Sub Workbook_Open()
    
    With Sheets("Log").Cells(1, 1)
        .Value = Format(Now, "DD/MM/YYYY HH:MM")
        .Offset(, 1).Value = Environ("UserName")
        .EntireRow.Insert
    End With
        
End Sub
However, your example is suggesting a reversed order to what your code is doing provides (Last opened at bottom of list, but your code places in row 1 before shifting down), if so alternative to consider:
Code:
Private Sub Workbook_Open()
    
    With Sheets("Log").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .Value = Format(Now, "DD/MM/YYYY HH:MM")
        .Offset(, 1).Value = Environ("UserName")
    End With
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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