Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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 Excel in Microsoft 365


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 Excel in Microsoft 365. 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

Suppressing Headers or Footers

Don't want a header or footer to appear on just a portion of your document? It's easy to do when you understand that ...

Discover More

Printing Very Large Paper Sizes

Need to print on large pieces of paper? Word has a limit on the size of the paper it can use, but that might not be the ...

Discover More

Skipping Numbering

Got a numbered list, but you want to add other types of non-numbered paragraphs in the middle of the list? It's easy to ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Seeing How a Worksheet Will Print While Editing

Sometimes it is necessary to print a worksheet to see how the data it contains appears. This may not be necessary, ...

Discover More

A Shortcut for Switching Focus

While not technically an Excel-only tip, the shortcuts described in this tip will help you switch focus from your ...

Discover More

Displaying a Hidden First Column

Hiding columns is easy, even hiding column A. How, then, do you get that left-most column displayed again? Here are a few ...

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 eight less than 8?

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.