2020-11-28

How to get the Windows user name with VBA

Problem

We need to get the Windows user name using VBA. For example, in Excel we can display different info depending on the user opening the Excel file.

Solución

  1. Open the Visual Basic Editor: press Alt+F11.
  2. Copy the following subroutine into a module.
    • Sub user()
      MsgBox Environ("UserName")
      End Sub
      
  3. Run the subroutines: press F5.
  4. Excel will display a pop up message showing the Windows user name.
We can also type the following text in the Immediate window and press Enter.

?Environ("Username")
Or we can capture and print the user name in a cell to customize the information displayed in that sheet or workbook.

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

References

No hay comentarios:

Publicar un comentario

Nube de datos