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.

ArrowMain

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

ArrowOutput

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 info@exceltip.com

Users are saying about us...

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube