28

In most of the online resource I can find usually show me how to retrieve this information in VBA. Is there any direct way to get this information in a cell?

For example as simple as =ENVIRON('User') (which did not work)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

6 Answers6

51

Based on the instructions at the link below, do the following.

In VBA insert a new module and paste in this code:

Public Function UserName()
    UserName = Environ$("UserName")
End Function

Call the function using the formula:

=Username()

Based on instructions at:

https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b-3622-40d6-a1b2-b6748ae8231f

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
13

Without VBA macro, you can use this tips to get the username from the path :

=MID(INFO("DIRECTORY"),10,LEN(INFO("DIRECTORY"))-LEN(MID(INFO("DIRECTORY"),FIND("\",INFO("DIRECTORY"),10),1000))-LEN("C:\Users\"))
sangorys
  • 1,161
  • 10
  • 16
  • 1
    This only works if you save the workbook in your user's directory. At work I save mine on a networked drive, so it shows part of the local IP address. – Travis Oct 04 '21 at 15:11
5

Example: to view the Windows User Name on Cell C5, you can use this script:

Range("C5").Value = ": " & Environ("USERNAME")
Nickolay
  • 31,095
  • 13
  • 107
  • 185
web solusi
  • 59
  • 1
  • 1
5

if you don't want to create a UDF in VBA or you can't, this could be an alternative.

=Cell("Filename",A1) this will give you the full file name, and from this you could get the user name with something like this:

=Mid(A1,Find("\",A1,4)+1;Find("\";A1;Find("\";A1;4))-2)


This Formula runs only from a workbook saved earlier.

You must start from 4th position because of the first slash from the drive.

Rafa Barragan
  • 602
  • 9
  • 24
  • I like this answer but sadly for documents pulled from the cloud the address does not contain the current user. – ΩmegaMan Oct 23 '18 at 16:53
5

This displays the name of the current user:

Function Username() As String
    Username = Application.Username
End Function

The property Application.Username holds the name entered with the installation of MS Office.

Enter this formula in a cell:

=Username()
rolacher
  • 68
  • 1
  • 4
  • Work at first try, thanks! It returned the full name as "Adam Smith". – Michael Larsson Aug 11 '22 at 09:21
  • The only real solution - after 5+ years, in a virtual and remote environment gives back my full name. ...where my login name is a 8 digit number, and my home dir is mapped to H:\ – V-Mark Feb 02 '23 at 17:00
3

The simplest way is to create a VBA macro that wraps that function, like so:

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

Then call it from the cell:

=UserNameWindows()

See this article for more details, and other ways.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130