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 Change Cell Color Excel supports more than 16 million colors in a cell; hence you should know how to set the exact color in a cell. To do this, you can use…
Learn how to write your first VBA macro in Excel to automate repetitive formatting tasks across multiple worksheets. This beginner-friendly guide will walk you through the process step-by-step, from recording your actions to customizing the code for your specific needs.
In MS Access, the best way to create a multiuser tool is to divide your solution. One part acts as interface and other one acts as database. You can have multiple copies of the interface distributed to users which are connected to central MS Access database saved at common shared drive. To connect the interface to database, you can use link table feature (Access>External Data>Import & Link) available in MS Access. Below is a commonly required VBA code which helps the developers to re-link MS Access linked tables when the database is renamed or moved to other location
QR Code Generator Tool in Excel Hi Friends, today we are going to learn something which is more closure to many businesses. As the businesses going global and digital, there is a need to adopt…
Dummy Data Generator is an MS Excel based tool which has capability of generating 45 types of data which includes numbers, text, date, time, Memo (long text), Boolean etc.
VBA Code To Add Items In Listbox Control Using ListBox in Userform is very common. You can use ListBox.AddItem function to add items in the listbox.; however, it is little difficult to add items in…
Does not work with Azure joined computers. Environ(“username”) appears to return blank
Hi,
Can you give an try to below code: