In this article we will learn about VBA code to get computer name.

Excel VBA, or Visual Basic for Applications, is a programming language that can be used to automate tasks within the Microsoft Excel software. One of the many tasks that can be automated using VBA is the retrieval of the computer name. The computer name is a unique identifier assigned to a computer on a network and can be useful in a variety of situations, such as keeping track of which computer a specific file was last saved on or for troubleshooting purposes.

VBA Code to Get Computer Name

The code that we will use to retrieve the computer name is the following:

Sub GetComputerName()

    Dim strComputerName As String
    strComputerName = Environ("COMPUTERNAME")
    
    Range("A1").Value = strComputerName
    MsgBox strComputerName

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 GetComputerName and in the last line we closed the sub-routine using End statement.

Dim strComputerName As String

The second line of this code declares a variable called “strComputerName” as a string.

strComputerName = Environ("COMPUTERNAME")

The third line assigns the value of the computer name to the variable “strComputerName” 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 “COMPUTERNAME,” which is the name assigned to the computer.

Range("A1").Value = strComputerName

We can then use this variable to insert the computer name into an Excel cell or to display it in a message box. The fourth line of code will insert the computer name into cell A1 on the active worksheet.

MsgBox strComputerName

The fifth line of code will display the computer name in a message box.

Steps to use this VBA Code to Get Computer Name

  • To retrieve the computer 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 Computer 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 Computer Name

  • Paste the VBA code in the module like shown below
    VBA Code to Get Computer 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 Computer Name
  • Give a name to the shape like ‘Get Computer Name’
    VBA Code to Get Computer Name
  • Right click on the shape and select ‘Assign Macro…’
    VBA Code to Get Computer Name
  • Select ‘GetComputerName’ from the list and click on ‘Ok’ button
    VBA Code to Get Computer Name
  • Done, now click on the button. It should insert the computer name in cell A1 and show a message box with the computer name
    VBA Code to Get Computer Name

Download Practice File

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

Conclusion

To summarize, retrieving the computer name using Excel VBA is a simple task that can be accomplished by using the Environ function and a few lines of code. This information can be useful in a variety of situations and can be easily integrated into a macro for convenience.

Note: The above code will work on Windows Operating System only, for other operating system it will need to use different function to get the computer name.

Similar Posts

Leave a Reply

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