Written by Allen Wyatt (last updated December 24, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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 2021. You can find a version of this tip for the older menu interface of Excel here: Grabbing a User's Name from Excel.
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!
Excel tries to anticipate what you want to type into a cell, particularly when it comes to entering formulas. Here are ...
Discover MoreExcel helpfully lets you know when the data or formulas you've entered in a cell don't make sense. It does this by ...
Discover MoreOpen a workbook, look at the data, start to close the workbook, and you are asked if you want to save your changes. What ...
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