I am creating a userform that will be used by a variety of users to input data into the one excel workbook. I would like the code to get the user name out of excel and onto the worksheet.
Thank you in advance.
Printable View
I am creating a userform that will be used by a variety of users to input data into the one excel workbook. I would like the code to get the user name out of excel and onto the worksheet.
Thank you in advance.
"Out of excel" :confused: What does that mean?
It is easy as pie to get the windows login name, but I've no clue what you mean by 'out of excel'
If, for instance, I look at my Excel Options (the one you access through the Office button in Excel 2007), or if I look at the options that are found at the bottom of the tools drop down menu in earlier versions of Excel the copy of Excel is registered to a particular user, who can then personalise it. This is the user name I want to "get out of" Excel. (This does, of course, assume that users are logged in to the system under their own user ID. This is a reasonably safe assumption. In the event that a user is logged in under someone else's ID and is actually working the ID owner would know about it.)
I've always used the windows logon name. In my shop, the user name is actually irrelevant. It is the same for 4,000 + people and that is the name of the company.
At any rate, I found this through Google. I think it is what you are looking for (it does return the name of the company I work at when I run it)Code:Private Sub CommandButton1_Click()
MsgBox Application.UserName
End Sub
There is also the Environ Function in VBA.
vb Code:
Function UserNameWindows() As String UserName = Environ("USERNAME") End Function
That returns the windows login, not the name registered in Excel.
Ah! yes, you are right Hack...
In my company, the login name is reflected in the applicaton.username for all users in Excel...
Edit: I just checked 3 users who are on a network, and it shows their domain login names when I use Application.Username. I guess, the IT guys have made it user-specific in my company :)
Here, your windows login is your company ID badge number so I'm C123456
Thanks everyone! Application.UserName works a treat!