VBA username vs userID

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,107
Office Version
  1. 365
Platform
  1. Windows
Good Day,

If I identify the TEST-FILE.xlsx file in its open folder through 'Shift + Right Click' for "Copy as path" I'm getting:

"C:\Users\user-id\Folder1\Folder2\Folder3\Folder4\TEST-FILE .xlsm"

Each username has a specific user-id.

Now, I'm having trouble identifying how to get each username's user-id so I can use it in a code.

If I open a Windows Explorer and click on each username's "This PC" in the left navigation panel I'm getting:

"C:\Users\user-id"

For example, if I right click on the open Windows Explorer window for Tod Smith, I would get something like "DR4EACE" for his user-id and get:

"C:\Users\DR4EACE"

So, how can I identify Tod Smith's user-id in using Excel?


Any thoughts, comments, articles, links, please let me know!

Thank you!
pinaceous
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Various ways to get to username and different types of username if you wish, below are a couple of examples of getting to the different usernames:

Excel application username
VBA Code:
Sub test()
    Debug.Print Application.UserName
End Sub

Environ username:
VBA Code:
Sub test2()
    Debug.Print Environ("username")
End Sub

One version of system username/ Domain/ FullName:
VBA Code:
Sub test3()
    Dim UserFullName As String
    Dim WSHnet As Object
    Dim objUser As Object
    Dim UserName As String
    Dim UserDomain As String

    Set WSHnet = CreateObject("WScript.Network")
    UserName = WSHnet.UserName
    UserDomain = WSHnet.UserDomain
    Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
    UserFullName = objUser.FullName
    
    Debug.Print UserName
    Debug.Print UserDomain
    Debug.Print UserFullName
End Sub

Hope this helps
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,182
Members
448,872
Latest member
lcaw

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