VBA to show the first username that opens the book

Joige

New Member
Joined
Jun 28, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I'm looking for a function that shows the windows username of the first person who opened the book.
Looking online I found two VBA that got me close to what I wanted, but I haven't managed to combine them since I understand close to nothing of VBA code.

I have a name "OpenFirstTime" set to TRUE and this code
VBA Code:
Private Sub Workbook_Open()
    If Evaluate("OpenFirstTime") = True Then
        'missing code
        ThisWorkbook.Names("OpenFirstTime").Value = False
    End If
End Sub
I also have this function that shows the CURRENT windows username
VBA Code:
Function UserName() As String
    UserName = Environ("USERNAME")
    
End Function
Again, I'm looking for a way to run the function on the first workbook open, save the result, and then, with another function, call that result.
Is that even possible?

Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Create a sheet named "Log". It can be hidden from user view if desired.

Paste the following into the 'ThisWorkbook' module in the VBE :

VBA Code:
Private Sub Workbook_Open()
Dim LR As Long

    With Sheets("Log")
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("A" & LR + 1).Value = "Opened"
        Range("B" & LR + 1).Value = Now
        Range("C" & LR + 1).Value = Environ("username")
    End With
    
End Sub
 

Attachments

  • Users.jpg
    Users.jpg
    42.5 KB · Views: 8
Upvote 0
Create a sheet named "Log". It can be hidden from user view if desired.

Paste the following into the 'ThisWorkbook' module in the VBE :

VBA Code:
Private Sub Workbook_Open()
Dim LR As Long

    With Sheets("Log")
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("A" & LR + 1).Value = "Opened"
        Range("B" & LR + 1).Value = Now
        Range("C" & LR + 1).Value = Environ("username")
    End With
   
End Sub
Thanks for the reply, but that's not really what i'm looking for.
I would prefer not to add anything to the workbook, I'd like to have the current user function which returns the current user when called in a formula UserName(), and another that returns the first user that opened the work bookFirstUserName().

Example:
I open the workbook the first time and both function return "Jorge". I save it and send it to a friend. When he opens it UserName() returns "Joe" and FirstUserName() returns "Jorge"

Sorry but it's hard to explain myself with 0 knowledge on VBA. Thanks!
 
Upvote 0
I don't know if this is even possible. If not, please let me know and I'll try to find another way to make this work.
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,184
Members
448,949
Latest member
keycalinc

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