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

Converting Numbers to Strings

VBA is great at working with both strings and numbers. At some point you may have a number you need to convert to a ...

Discover More

Updating to Smart Quotes

As you type a document, Word automatically converts your quote marks and apostrophes to "curly" versions that look more ...

Discover More

Permanent Watermarks in a Document

Need to add a graphic watermark to a document? It's not that hard to do but making the watermark permanent can be a bit ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Selecting a Suggestion with the Keyboard

Excel tries to anticipate what you want to type into a cell, particularly when it comes to entering formulas. Here are ...

Discover More

Modifying Error Alerts Received

Excel helpfully lets you know when the data or formulas you've entered in a cell don't make sense. It does this by ...

Discover More

Saving Non-Existent Changes

Open a workbook, look at the data, start to close the workbook, and you are asked if you want to save your changes. What ...

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 7 - 7?

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.