by Allen Wyatt
(last updated March 1, 2014)
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:
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:
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 (7251) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Need to test your formulas? Then you need some testing data that you can use to see if the formulas function as you expect. ...Discover More
Excel keeps track of a range of stats about each workbook you use. If you want to take a look at those stats, it's easy; just ...Discover More
When keeping track of dates, internally, Excel uses special date serial numbers that are based upon a specific day in the ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.