Mostrando entradas con la etiqueta Environ. Mostrar todas las entradas
Mostrando entradas con la etiqueta Environ. Mostrar todas las entradas

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

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

2017-06-07

Obtener el nombre de usuario de Windows con VBA en Excel

Problema

Necesitamos conocer el nombre de usuario de Windows en Excel. Por ejemplo, porque en función del mismo, mostraremos una información distinta al usuario que abra el fichero de Excel.

Solución

  1. Abrimos el Editor de Microsoft Visual Basic: Alt+F11.
  2. Copiamos la siguiente subrutina en un módulo.
    • Sub usuario()
      MsgBox Environ("UserName")
      End Sub
      
  3. Ejecutamos la subrutina: F5.
  4. Aparecerá un mensaje emergente con el nombre de usuario de Windows.
También podemos simplemente teclear en la ventana de inmediato:

?Environ("Username")
O capturar el nombre en una celda para personalizar la hoja en función de la misma.

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

Referencias

Nube de datos