USERNAME

USERNAME()
Returns either the application user name or the network domain name.

sTypeThe type of username you want to return.

REMARKS
The application username can be seen from the (Tools > Options)(General tab).
The network username can be seen from ..
UserName("Application") = "Matthew Smith"
UserName("Domain") = "smithm"

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

Public Function USERNAME( _
         ByVal sType As String) _
         As String

Dim sBuffer As String * 255
Dim lLength As Long
Dim sUserName As String

   If (UCase(sType) = "APPLICATION") Then
      UserName = Application.UserName
   End If
   
   If (UCase(sType) = "DOMAIN") Then
      sUserName = ""
      lLength = GetUserName(sBuffer, 255)
      lLength = InStr(1, sBuffer, Chr(0))
      If (lLength > 0) Then
          sUserName = Left(sBuffer, lLength - 1)
      Else
          sUserName = sBuffer
      End If

      USERNAME = UCase(Trim(sUserName))
    End If
End Function

For instructions on how to add this function to a workbook refer to the page under Inserting Functions


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext