Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Grabbing a User's Name from Excel.

Grabbing a User's Name from Excel

Written by Allen Wyatt (last updated December 24, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


2

Mark has a worksheet where he wants to record the name of a user, but rather than asking the user to fill in a form, he wants to automatically grab their username from Excel.

The username that a person sets in Excel when first installing the software or when changing the general options for the program cannot be accessed via formula. Instead, you need to use a macro to access the information and then make it available to your worksheet. This is possible through the use of a user-defined function. Consider the following simple example:

Function GetUserName()
    GetUserName = Application.UserName
End Function

Note that the macro does nothing more than to access the UserName property of the Application object. You use this function in your worksheet in the following manner:

=GetUserName()

With this simple formula in a cell, the username is displayed in the cell.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9814) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Grabbing a User's Name from Excel.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Marking Multiple Documents

After using Word for a while, it is easy to accumulate quite a few documents. At some point you may want to make a change ...

Discover More

Boxes in Boxes

When you insert a text box within another text box, you may expect any text in the outer text box to wrap around the ...

Discover More

Copying Paragraph Formatting with the Mouse

When you get one paragraph formatted just the way you want, you might want to copy that formatting so it can be applied ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Seeing Excel's Program Window

Have you ever opened Excel to find that the window you saw yesterday is not the same as it is today? Sometimes, for ...

Discover More

Status Bar Summing No Longer Available

When you select a range of cells, Excel normally displays the sum of those selected cells on the status bar. If the sum ...

Discover More

Generating Random Testing Data

Need to test your formulas? Then you need some testing data that you can use to see if the formulas function as you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five less than 9?

2024-12-26 10:44:40

J. Woolley

Brian's point is interesting, but I think he meant Application.UserName is a read/write property, not read/variable. I was surprised it is so easily changed. With this VBA statement
    Application.UserName = "Santa Claus"
I became Excel's Santa Claus even after restarting my computer. I had to repeat the statement with my actual name to avoid further confusion.
My Excel Toolbox includes the NameOf function described in my comment here:
https://excelribbon.tips.net/T006145#comment-form-hd
This formula returns Excel's Application.UserName:
    =NameOf("user")
And this formula returns the Windows environment variable USERNAME:
    =NameOf("USERNAME")
The latter result is the same as Brian's VBA statement Environ("Username"). In my case it is the first word of my Windows login name, which has two words. It is also the name of the subfolder under C:\Users where Windows put my Documents and Desktop folders. This formula returns the path to that subfolder:
    =NameOf("USERPROFILE")
And this formula returns the same path without its "C:" disk drive:
    =NameOf("HOMEPATH")
My Excel Toolbox also includes the following dynamic array function to return all Windows environment variables and their values in 2 columns and N rows (including an optional header row):
    =ListEnvironVariables([SkipHeader])
See https://sites.google.com/view/MyExcelToolbox/


2024-12-24 08:20:12

Brian

This macro returns the name the user has set in the excel options - not specifically the windows login name. It's a read/variable, so if a macro that has a line: Application.UserName = "Santa Claus" it will work and probably upset Mark's workbook.

An alternative is Environ("Username") which returns the user name as recorded by Windows when the user logged in. I believe (but happy to be corrected) that this can't be changed by the user.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.