函数返回在Excel中使用VBA的用户名
|下面的函数将返回登录用户的用户名,在Windows NT和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
如果您不想返回网络用户名,但想返回用户已在应用程序中注册的用户名,则可以执行以下操作:
ActiveUserName = Application.UserName