VBA Code to Get User Domain Name
VBA Code to Get User Domain Name in Excel
VBA Code to Get User Domain Name – Method 1
Here is the first example of VBA code that can be used to retrieve the user’s domain name:
Sub GetUserDomain_Method1()
Dim strUserDomain As String
strUserDomain = Environ("USERDOMAIN")
Range("A1").Value = strUserDomain
MsgBox strUserDomain
End Sub
Explanation of the Code
Dim strUserDomain As String
The second line of this code declares a variable called “strUserDomain” as a string.
strUserDomain = Environ("USERDOMAIN")
The third line assigns the value of the user domain name to the variable “strUserDomain” by using the Environ function. The Environ function is a built-in VBA function that retrieves the value of a specified environment variable. In this case, the environment variable that we are retrieving is “USERDOMAIN” which is the domain name assigned to the computer.
Range("A1").Value = strUserDomain
We can then use this variable to insert the domain name into an Excel cell or to display it in a message box. The fourth line of code will insert the domain name into cell A1 on the active worksheet.
MsgBox strUserDomain
The fifth line of code will display the domain name in a message box. Few Things to Keep in Mind
VBA Code to Get User Domain Name – Method 2
Sometimes method 1 does not work for few users, in that case you can try using following alternate VBA code to get the domain name using Wscript.Network. The Wscript.Network object is a COM (Component Object Model) object that can be used to access various network properties and methods.
Sub GetUserDomain_Method2()
Dim objNetwork As Object
Dim strUserDomain As String
Set objNetwork = CreateObject("Wscript.Network")
strUserDomain = objNetwork.UserDomain
Range("A1").Value = strUserDomain
MsgBox strUserDomain
End Sub
Explanation of the Code
This is a VBA code that retrieves the user’s domain name and displays it in a message box as well as in cell A1 of the active worksheet. Here’s a breakdown of the code:
- The first line “Sub GetUserDomain_Method2()” declares the start of a subroutine (or sub-procedure) called “GetUserDomain_Method2”. Subroutines are used to group together a set of instructions that perform a specific task.
- The next two lines “Dim objNetwork As Object” and “Dim strUserDomain As String” declares two variables named “objNetwork” and “strUserDomain” respectively.
- The next line “Set objNetwork = CreateObject(“Wscript.Network”)” creates an instance of the “Wscript.Network” object and assigns it to the “objNetwork” variable.
- The next line “strUserDomain = objNetwork.UserDomain” assigns the value of the “UserDomain” property of the “objNetwork” object to the “strUserDomain” variable. The “UserDomain” property returns the user’s domain name as a string.
- The next line “Range(“A1″).Value = strUserDomain” assigns the value of the “strUserDomain” variable to cell A1 of the active worksheet.
- The last line “MsgBox strUserDomain” displays the value of the “strUserDomain” variable in a message box.
- The last line of the code is “End Sub” which is used to mark the end of the subroutine.
Steps to use this VBA Code to Get User Domain Name
- To retrieve the domain name using VBA, we will first need to open the Microsoft Excel application and access the VBA editor. This can be done by pressing the “Alt + F11” keys on the keyboard or by clicking on the “Developer” tab in the Excel ribbon and then selecting “Visual Basic.”
- Once the VBA editor is open, we will need to create a new module by clicking on the “Insert” tab and selecting “Module.” This will create a new module where we can enter our VBA code.
- Paste the VBA code in the module like shown below
- Now we need a shape in the Excel sheet which will be used as a button to call this code. For that, add a shape in Excel sheet
- Give a name to the shape like ‘Get User Domain’
- Right click on the shape and select ‘Assign Macro…’
- Select ‘GetUserDomain_Method1’ or ‘GetUserDomain_Method2’ (depends on which works on your system) from the list and click on ‘Ok’ button
- Done, now click on the button. It should insert the user domain name in cell A1 and show a message box with the user domain name
Download Practice File
Conclusion
Both of the codes can be used to retrieve the user’s domain name, but the first one uses the built-in “Environ” function, while the second one uses the “Wscript.Network” object. Both of them can be used in any VBA supported application such as Excel, Word, PowerPoint, etc.