Inserting the User's Name in a Cell

by Allen Wyatt
(last updated January 11, 2017)

4

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12745) applies to Microsoft Excel 2007, 2010, and 2013.

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

Selecting Formulas

Want to select only the formulas in your worksheet? It's easy to do using the Go To Special dialog box.

Discover More

Pasting HTML without Hyperlinks

Excel allows you to copy information from the web and paste it into a worksheet. Problem is, the pasting could take some time ...

Discover More

Suppressing ASK Fields When Printing

Do you like using ASK Fields in your documents to get information from the user but don't want Word to update the fields more ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Clearing Large Clipboard Entries

Need to clear out a large amount of information saved on the Clipboard? All you need to do is to replace it with a small ...

Discover More

Creating New Windows

If you need to look at different parts of the same worksheet at the same time, the answer is to create windows for your data. ...

Discover More

Spotty Recalculation

Does your worksheet or workbook not always recalculate like you expect? If so, then some of the ideas in this tip may help ...

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}] 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 - 0?

2017-09-15 02:49:11

lemmens freddy

Hé Eduaro, what a great idea ! It works fine for me, but I had to tick on 'Use relative Reference' and in the comment box I used Ctrl + Home and then Ctrl + Shift + End to select the whole name and then Ctrl + C. Thx again!


2017-08-02 14:34:17

Renam Eduardo Pereira

Hello everybody.
I found a way too simple to do that (I had searches in whole web, and no source code that I found did work for me.
So, I thought and I'e created a "recorded macro", like this:
a. Create a macro called "Username" (for example);
b. Go to the desired cell and add a comment.
Note that the first thing that appears is the username, right? Select it and CTRL+C
c. Return to the cell and paste the value.
d. Remove the comment
e. Stop the macro

So, you can do log control, user control, and so on.

I hope this help anyone here.

Best regards!
Eduardo


2017-01-11 08:53:21

Chris

How would the UserName2() code (and its Declaration stt's) need to be modified to support 64-bit Excel?

I've tried but using:

#If VBA7 Then
Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As LongLong) As LongLong
#Else
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
#EndIf

gets flagged for a compile error that I haven't been able to figure out.


2013-11-25 08:39:02

Bryan

You don't even need the API call. You can do the whole thing in a one-line macro:

Function UserName3() as String
UserName3 = Environ("UserName")
End Function


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.