VBA Code to Get User Domain Name

VBA Code to Get User Domain Name in Excel

VBA (Visual Basic for Applications) is a programming language that can be used to automate tasks in Microsoft Office applications such as Excel, Word, and PowerPoint. One way to use VBA is to write code that retrieves information about the user’s computer, such as the user’s domain name.

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

The first and last line of this codes are sub-routine definition. In the first line we created a sub-routine named GetUserDomain_Method1 and in the last line we closed the sub-routine using End Sub statement.
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

Keep in mind that this code only works when run on a computer that is connected to a domain. If the computer is not connected to a domain, the “Environ” function may return an empty string. Additionally, this code will only work when run on a Windows operating system.

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.”
    VBA Code to Get User Domain Name
  • 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.
    VBA Code to Get User Domain Name
  • Paste the VBA code in the module like shown below
    VBA Code to Get User Domain Name
  • 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
    VBA Code to Get User Domain Name
  • Give a name to the shape like ‘Get User Domain’
    VBA Code to Get User Domain Name
  • Right click on the shape and select ‘Assign Macro…’
    VBA Code to Get User Domain Name
  • Select ‘GetUserDomain_Method1’ or ‘GetUserDomain_Method2’ (depends on which works on your system) from the list and click on ‘Ok’ button
    VBA Code to Get User Domain Name
  • 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
    VBA Code to Get User Domain Name

Download Practice File

You can also practice this through our practice files. Click on the below link to download the 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.

Similar Posts

Leave a Reply

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