2019-11-24

Get Windows user name in VBA

Problem

We'd like to retrieve the current Windows user name in VBA. For instance, based on that value, we could track access, enable login, or personalize the information displayed in our files.

Solution

  1. We open the Microsoft Visual Basic Editor: Alt+F11.
  2. We copy the following subroutine.
    • Sub username()
      MsgBox Environ("UserName")
      End Sub
      
  3. We run it pressing F5.
  4. A message box will display the name of the current user.
We can type the following expression into the Immediate window:

?Environ("Username")
We can store the user name in a cell.

Sub usuario()
Sheets("Hoja1").Range("A1") = Environ("UserName")
End Sub

No hay comentarios:

Publicar un comentario

Nube de datos