ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   How to get a user's user id in an Excel macro (https://www.excelbanter.com/excel-worksheet-functions/191739-how-get-users-user-id-excel-macro.html)

GEP

How to get a user's user id in an Excel macro
 
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 ToolsShared Workbook, for example.
Thanks!

ExcelBanter AI

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

1. [b]Open the Visual Basic Editor[/bby pressing Alt F11.
2. In the editor, [b]insert a new module[/bby clicking on Insert  Module.
3. In the module, [b]create a new sub[/bby typing "Sub GetUserID()" followed by pressing Enter.
4. Inside the sub, [b]declare a variable[/bto hold the user id by typing "Dim UserID as String" followed by pressing Enter.
5. Use the [b]Environ function[/bto retrieve the username by typing "UserID = Environ("USERNAME")" followed by pressing Enter.
6. You can now use the [b]UserID variable[/bin your macro to check if the user id matches a specific value. For exampleyou can use an If statement like this:

   If 
UserID "SmithJo" Then
       
'Do something
   Else
       '
Do something else
   
End If

7. [b]Save your macro[/b] and close the Visual Basic Editor

Now, when the macro is executed, it will retrieve the user id of the person running the macro and store it in the UserID variable. You can then use this variable to perform actions based on the user id.

Tom Hutchins

How to get a user's user id in an Excel macro
 
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

"GEP" wrote:

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 ToolsShared Workbook, for example.
Thanks!


GEP

How to get a user's user id in an Excel macro
 
Hutch, Thanks! The simple way worked great for me.

"Tom Hutchins" wrote:

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

"GEP" wrote:

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 ToolsShared Workbook, for example.
Thanks!



All times are GMT +1. The time now is 02:33 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com