Sometimes in VBA projects, a programmer is required to control the access on the data or sheets. This can be achieved using two ways:
In the first method, we can make use of Excel VBA inbuild function named Environ to get the user name of the current logged-in user. This is the most common code used by the developers.
Sub GetLoggedInUserName()
'Declare variable
Dim strUserName As String
'Get system logged in user name
strUserName = Environ("Username")
'Display the user name on sheet1
Sheet1.Range("C4").Value = strUserName
End Sub
Second method is to use Application.UserName property. Note that it may not work sometime as it tries to get user details from the installed Excel application.
Sub Get_Username()
Sheet1.Range("C5").Value = Application.UserName
End Sub
Now a day few developers started reporting that both method 1 and 2 goes not work for few users. Here we can make use of below code which uses network object to get user details.
Function CurrentUser()
Dim objNetwork As Object
Dim strUserName As String
Set objNetwork = CreateObject("Wscript.Network")
strUserName = objNetwork.UserName
Sheet1.Range("C6").Value = strUserName
End Function
To use this code in your Excel file, follow below steps:
1. Open an Excel file
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Get Logged In User Name’
7. Right click on the shape and select ‘Assign Macro…’
8. Select ‘GetLoggedInUserName’ from the list and click on ‘Ok’ button
9. Done, click on the shape to get the logged in user name
Hope you liked this article!!
Here are some other VBA codes which you can use in Excel:
Here are some other free Excel VBA Tools which may help you to increase productivity in your day to day jobs. Click here
VBA Code to send Outlook Emails Sending bulk emails is a very common activity, there are many office activities that need a person to send bulk emails to single or multiple recipients. You also may…
Have you ever got into situation in office where you need to count the cells in Excel sheet with specific color? If yes then you can use following code which counts the number of cells…
Colorindex in Excel VBA Today let’s try to understand how ColorIndex property in Excel VBA works. It is an easy and effective way to quickly complete the development. ColorIndex property is normally used by VBA…
Time Management is very effective way of managing the available time. Current competitive world is making the time management crucial. There are various ways, tools and techniques by which time management can be done easily.
VBA Tools To Create Folders In VBA, you can use MkDir function to create folders in your system or shared drive. To make your work simple, we bring a free Excel VBA tool to create…
VBA Code to Count Color Cells With Conditional Formatting Have you ever got into situation in office where you need to count the cells with specific color in conditional formatted Excel sheet? If yes then…
Does not work with Azure joined computers. Environ(“username”) appears to return blank
Hi,
Can you give an try to below code: