VBA Code to Get User Name

How to Get username in VBA ?

Sometimes in VBA projects, a programmer is required to control the access on the data or sheets. This can be achieved using two ways:

1. Design a login userform and display it on the open event of the workbook. Based on the credentials, you can control the access on the data
2. Instead of using login userform, you can get the system login user id and control the access on the data

Method 1: Get Username in VBA Code

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
				
			

Method 2: Get UserName Syntax Code

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
				
			

Method 3: Wscript.Network

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ā€¦ā€™

Get logged in User form in Excel VBA

8. Select ā€˜GetLoggedInUserNameā€™ from the list and click on ā€˜Okā€™ button

Get logged in User form in Excel VBA

9. Done, click on the shape to get the logged in user name

Get logged in User form in Excel VBA

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Hope you liked this article!!

Here are some other VBA codes which you can use in Excel:

Recommended Articles

Automate Tasks using Excel VBA Utility Tools

Here are some other free Excel VBA Tools which may help you to increase productivity in your day to day jobs. Click here

Similar Posts

2 Comments

  1. Does not work with Azure joined computers. Environ(“username”) appears to return blank

    1. Hi,

      Can you give an try to below code:

      Function CurrentUser()
      
          Dim objNetwork As Object
          Dim strUserName As String
          
          Set objNetwork = CreateObject("Wscript.Network")
          strUserName = objNetwork.UserName
          
          MsgBox strUserName
          
      End Function

Leave a Reply

Your email address will not be published. Required fields are marked *