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.
Written by Allen Wyatt (last updated December 24, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
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!
Sometimes it is necessary to print a worksheet to see how the data it contains appears. This may not be necessary, ...
Discover MoreWhile not technically an Excel-only tip, the shortcuts described in this tip will help you switch focus from your ...
Discover MoreHiding columns is easy, even hiding column A. How, then, do you get that left-most column displayed again? Here are a few ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments