How to get a user's user id in an Excel macro

  • Thread starter Thread starter GEP
  • Start date Start date
G

GEP

Is there a way for me to identify in an Excel macro the user id of the person
trying to execute the macro? For example, If User_ID = SmithJo Then (where
User_ID is something that Excel would make available to me somehow). I know
Excel knows user ids that users use to log into their PCs and/or network
since it displays them when I click on Tools>Shared Workbook, for example.
Thanks!
 
The simplest way is
Dim UserName as string
UserName = Environ("USERNAME")

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

You would call it like this:
Sub AAAAA()
MsgBox UserName()
End Sub

Finally, you could also try retrieving Application.UserName, which is the
name
entered on the Tools >> Options >> General tab.

Hope this helps,

Hutch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top