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: Inserting the User's Name in a Cell.

Inserting the User's Name in a Cell

Written by Allen Wyatt (last updated October 31, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


6

Sunlim noted that when Office is installed, the user specifies their name. This name can be accessed in some Office programs, such as in Word. Sunlim wonders how he can access the user's name in Excel and place that name in a cell.

The way to do this is to implement a short, one-line macro that accesses the UserName property of the Application object. This technique is detailed in a different issue of ExcelTips:

http://excelribbon.tips.net/T009814

That approach is great at determining the user name associated with the current installation of Excel. However, that may not be the same thing as who is using the current workbook. For instance, if the workbook is shared, it is possible that multiple people could be using it at the same time. In that case, you need a way to determine those names, as shown here:

Function UserNames() As String
    Dim Users As Variant
    Dim sMsg As String
    Dim iIndex As Integer

    Users = ActiveWorkbook.UserStatus

    For iIndex = 1 To UBound(Users, 1)
        sMsg = Users(iIndex, 1) & vbLf
    Next iIndex
    'remove final line feed
    sMsg = Left(sMsg, Len(sMsg) - 1)

    UserNames = sMsg
End Function

To use the function, just enter the following formula in the cell where you want the names to appear:

=UserNames

If you instead want to know who is using the computer currently, it is best to look beyond Office and instead grab the name from Windows itself. In that way you can determine who is logged in to Windows and use that as the user name. This takes an API function call declaration, but is otherwise relatively easy:

Private Declare Function GetUserName Lib "advapi32.dll" _
  Alias "GetUserNameA" (ByVal lpBuffer As String, nSize _
  As Long) As Long

Function UserName2() As String
    Dim strBuff As String * 100
    Dim lngBuffLen As Long

    lngBuffLen = 100
    GetUserName strBuff, lngBuffLen
    UserName2 = Left(strBuff, lngBuffLen - 1)
End Function

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 (12745) 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: Inserting the User's Name in a Cell.

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

Searching for Paragraph Marks and Line Breaks

Word allows you to search not for special characters that normally do not print such as paragraph marks and line breaks.

Discover More

Setting Paste Default to Use Target Formatting

There are several ways that Word can handle formatting when you paste information in a document. This tip shows you how ...

Discover More

Protecting Tracked Changes

Track Changes is a great tool for editors and collaborators to use when creating documents. An author, seeking changes ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Help for Older Excel Versions

If you are using an older version of Excel, you may discover one day that the online help system no longer works. This ...

Discover More

Understanding R1C1 References

Referring to cells is typically done using a letter and a number, which represent the column and row. That's not the only ...

Discover More

Reference Shortcut

Need to modify how a cell reference, in a formula, is constructed? The shortcut described in this tip will help you step ...

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 two more than 7?

2021-05-05 15:52:56

J. Woolley

You might also be interested in this freely available array function in My Excel Toolbox:
=ListUserStatus()
Name, last activity (date/time), and type (exclusive/shared) are listed for each user that has the workbook open. It is most useful as a dynamic array in newer versions of Excel. You can also use it like this in older versions of Excel that do not support dynamic arrays:
=SpillArrayListUserStatus())
SpillArray will determine and populate the spill range for its array expression argument, simulating a dynamic array.
See https://sites.google.com/view/MyExcelToolbox/


2021-05-05 10:11:44

J. Woolley

@Connor
You must put the code in a standard code module, not the ThisWorkbook module. Pick Insert > Module from the VBA Editor's menu, then move the code to the new module.


2021-05-04 10:16:15

Connor MB

(see Figure 1 below)
(see Figure 2 below)
When I use the code above I get an error in excel

Figure 1. excel

Figure 2. code


2019-04-20 12:23:43

J. Woolley

@Jerrod: There is an error in the Tip after the paragraph "To use the function...." The cell formulas should include parentheses like this:
=UserNames()
=UserName2()
=UserName3()

In my case, UserNames() yields my first and last name but the other two functions yield only my first name. Here is another function that yields both names.

Function UserName4() As String
UserName4 = Application.UserName
End Function


2019-04-20 11:36:54

J. Woolley

Here is another way to get the user name, but it might yield only the user's first name:

Function UserName3() As String
UserName3=Environ("USERNAME")
End Function


2019-04-20 10:26:58

Jerrod Mason

Please show an example of using the Windows API to fetch user name. I can't get it to work.
Thanks!


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.