VBA to log times workbook has been opened into rows ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
773
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
773
Office Version
  1. 365
Platform
  1. Windows
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

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
773
Office Version
  1. 365
Platform
  1. Windows
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

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,907
Office Version
  1. 365
Platform
  1. Windows
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

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
773
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
Thats much better
Thanks
 
Upvote 0

Forum statistics

Threads
1,195,671
Messages
6,011,075
Members
441,581
Latest member
rp4717

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
Top