There are generally two user names that you can access via VBA. The first is the user name that is entered under Tools>Options>General. This one is accessed by using the UserName property of the Application object.
Function GetXLUserName() As String
‘Returns the user name entered under
GetXLUserName = Application.UserName
The second is the Windows user name. To get to that one, you need an API (Application Programmer’s Interface) function call. First define the API function like this
Declare Function apiGetUserName Lib “advapi32.dll” Alias _
“GetUserNameA” (ByVal lpBuffer As String, _
nSize As Long) As Long
Then use this API function call in your own function
Function GetUserName() As String
Dim sBuff As String * 25
Dim lBuffLen As Long
lBuffLen = 25
apiGetUserName sBuff, lBuffLen
GetUserName = Left(sBuff, lBuffLen – 1)
If you want to better understand how this API function call works, read Robert Rosenberg’s excellent post on the subject.