How do i retrieve Windows User name on a excel sheet?

  • Thread starter Thread starter Sirigeri Goutam Shetty
  • Start date Start date
S

Sirigeri Goutam Shetty

I'm trying to retrieve windows user name or log in name on a excel sheet with
certain commands. But i'm not sure if excel supports this feature.

Currently i'm using MS Office 2003 and WindowsXP professional edition.
 
From the internet (www.mrexcel.com) :

Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function

Sub test()
[A1] = GetUserName
End Sub

Daniel
 
As clarification, Daniel's code needs to go in the VBA editor. Right click
the sheet tab, select view code, and then paste the code in.
 
Just to clarify a little further, it doesn't all go there. This bit has to go
in a module in 'This Workbook'

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

Id go for something simpler.

Range("A1").Value = Environ("Username")

Mike
 
You can't do it directly from a formula -- you need VBA code. User

Function UserName() As String
UserName = Environ("UserName")
End Function

Then, call that function from a worksheet cell:

=UserName()

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top