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 check if folder exist Validation is one of the important parts of any programming language. As per few studies, 60% of the code is focused on validating input or output. In this…
Outlook Bulk Email Tool is an Excel and Outlook based tool which helps you to send or draft email in bulk right from Excel. It reads the recipient details from Excel sheet and uses Outlook installed on your system to generate emails. The tool supports To, Cc, Subject, Email Body, Attachment, HTML Table in Email Body.
VBA to Read Excel Data Using Connection String Sometimes as a programmer you need to read heavy (more then 5 MB) Excel files. There are two ways you can read data from Excel files: Open…
In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder
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.
Through formatting the cells, you can make your Excel data more beautiful and easier to understand. If you record a macro to add borders to Excel range, you will notice that it generates 30 plus lines of code for just a small work. Here we are sharing one line of code which does the same thing.
Does not work with Azure joined computers. Environ(“username”) appears to return blank
Hi,
Can you give an try to below code: