» Function for returning the user name using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Custom Functions in VBA
VERSION - All Microsoft Excel Versions
The function below will return the user name for the logged on user, works in both Windows NT and Windows95/98.
Public Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function ReturnUserName() As String
' returns the NT Domain User Name
Dim rString As String * 255, sLen As Long, tString As String
tString = ""
On Error Resume Next
sLen = GetUserName(rString, 255)
sLen = InStr(1, rString, Chr(0))
If sLen > 0 Then
tString = Left(rString, sLen - 1)
Else
tString = rString
End If
On Error GoTo 0
ReturnUserName = UCase(Trim(tString))
End Function
If you don't want to return the network user name, but want to return the user name that the user has registered with the application you can do this:
ActiveUserName = Application.UserName
Book Store:
Recommended Books:
- The Intelligent Investor: The Definitive Book On Value Investing, Revised Edition
- The Essential 55: An Award-Winning Educator's Rules for Discovering the Successful Student in Every Child
- Managerial Accounting: Tools for Business Decision Making, WebCT, 2nd Edition
- The Interpretation of Financial Statements
- Treason: Liberal Treachery from the Cold War to the War on Terrorism
- Investing in Real Estate, Fourth Edition
No comments have been submitted.

