How to Get Username using VBA?
Using Excel VBA get username who is currently active. Also get author name who edited the Excel workbook.
Both these are 2 different things. Is it Windows Active User or Workbook Author?
Purpose Command VBA: Get Active User details using Application Method Application.username Using Environment Variable Environ(“Username”) Using Environment Variable Environ$(“Username”) Get the Author name or Username who edited an Excel workbook ThisWorkbook.
Well that’s piece of cake if you know bit of VBA already.
But, What if I say there are more ways!!!
Would You be interested in knowing them as well?
Lets find out.
Different Method to get Windows UserName with VBA
Let’s see the easiest one first.
- System Environment Variables
- Windows API ‘apiGetUserName’ Function
- Network UserName
In some cases, if the easiest one did not work out, follow the next possible method.
1. Insert Username in Excel Cell
There is no built in function to get username to a worksheet cell. This has to be done by defining a user defined function. To do this, press Alt+ F11, go to VB editor, insert a new module & enter this code.Function to Get Username to Excel CellVisual Basic12345Function GetUserName() As StringGetUserName = Environ$("username")'or'GetUserName = Application.UserNameEnd Function
Then open the worksheet, then insert this formula “=GetUserName()” and press “Enter” key.
Now, the cell will execute the function and display the Windows logged in username in the cell.
Well this is the easiest of all the method and does not require any references to be added to any library or DLL.
Just copy-paste this code to Excel or Word VB Editor and run it.Visual Basic1234567Sub GetUserName_AppUser()Dim strUserName As String'Use the Application Object to get the UsernamestrUserName = Application.UserNameMsgBox "Current Logged In UserName is:" & strUserNameEnd Sub
This Excel macro code will display the current Windows username, who is also executing this Excel or Word App.
Also Read: VBA Option Explicit Not Working - Solved
3. Excel VBA Get Username from System Environment Variables
Open Command prompt and type ‘Set’ and press Enter. This command will display list of all the Environment variables and values stored in it. In this list, we have an Environment variable “USERNAME” and it will have the active logged in username for that session.
We can write macro to read this Environment Variable and get the user id.Visual Basic123456789101112Sub GetUserName_Environ()Dim idx As Integer'To Directly the value of a Environment Variable with its NameMsgBox VBA.Interaction.Environ$("UserName")'To get all the List of Environment VariablesFor idx = 1 To 255strEnvironVal = VBA.Interaction.Environ$(idx)ThisWorkbook.Sheets(1).Cells(idx, 1) = strEnvironValNext idxEnd Sub
Note: This VBA macro can also be used to read all the Environment variables from system for that login session.
4. Using Windows API ‘apiGetUserName’ Function
Microsoft provides many other variants of this function. So, you can choose to use this function or any other available
APIs based on your need. To use this API, declare the function before calling the function.Visual Basic12345678910111213141516Sub Get_UserName_WINAPI()'Declare VariablesDim Returned_Val_Length As LongDim API_Return_Status As LongDim Returned_UserName As String'Define Space for VariablesReturned_UserName = String$(254, 0)Returned_Val_Length = 255'Call the API and get the User DetailsAPI_Return_Status = apiGetUserName(Returned_UserName, Returned_Val_Length)'Remove unwanted details from Return value and get only UsernameReturned_UserName = Mid(Trim(Returned_UserName), 1, Returned_Val_Length - 1)End Sub
Also Read: VBA to Delete File in a Folder
5. Network UserName in Excel VBA
The WScript Object is used here to get the Network Username for the current logged in user.
Create an instance for the object Wscript.Network and use it to fetch the User and Computer Details as explained below.Visual Basic12345678Sub GetUserName_Environ()Dim ObjWshNw As ObjectSet ObjWshNw = CreateObject("WScript.Network")MsgBox ObjWshNw.UserNameMsgBox ObjWshNw.ComputerNameMsgBox ObjWshNw.UserDomainEnd Sub
All these combination of codes, fetch current Windows user name details to Excel.
We will be in need of these code tricks when, We share any Office document with other users and we need to collect the user details whoever is accessing it or restrict users from using this document based on their login id.
- Get list of Tasks running in System from Task Manager.
- Fetch Details from Websites to Excel Through VBA.
- Windows API detail from Microsoft Support Website.
- Fetching detail from Environment Variable.
- Network User details from WScript.Network.
Excel VBA Get Username – Windows Current User Name – File Author
- Games VCF Countif Record Macro Timer in Excel Task Manager print screen HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush Block Apps in FaceBook C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel outlook vba mass email Data Mining Excel Macro Facebook WhatsApp python Wordpress