Macro to return user name and computer name in Microsoft Excel

In this article, we will create a macro which will return computer and user name for the logged in user.

In this example, we want to extract computer name in cell C10 and user name in cell C11.


We have created “GetComputerUserName” macro to find the computer and user name. We can run the macro by clicking “Submit” button.


Macro will extract computer name in cell C10 and user name in cell C11.

Code explanation

Environ Function

Environ function returns the string value associated with environment variable of the operating system.

Syntax of Environ function

Environ({Environment variable name | number})


Please follow below for the code

Sub GetComputerUserName()

Dim ComputerName, UserName As String

'Getting computer name
ComputerName = Environ("computername")

'Getting user name
UserName = Environ("username")

'Assigning value to cell C10 and C11
Range("C10").Value = ComputerName
Range("C11").Value = UserName

End Sub


If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at


  1. Once the details are captured how do we clear the data from TextBox.

    I have used below Code on command button but nothing happens.

    Textbox1.value = “”

    • I haven't used any textbox here Parvez. if you are trying to empty the cells you can use Range("C10").value= "" and Range("C11").value=""

Leave a Reply

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

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.