Others

Get the Current Windows User ID in Excel or Access VBA

Tauseeq MagsiTauseeq Magsi Nov 27, 2026 869 views

Question details

Get the current Windows login name in Excel or Access VBA so it can be used to filter records or reports.

How to Get the Current Windows User ID in Excel or Access VBA
Product
Microsoft Excel and Microsoft Access
Device & OS
Windows
Scenario
A user wants a VBA procedure to identify the current Windows user and show records assigned to that login name.
Observed behavior
The VBA procedure returns the current Windows login name for use in a filter or report.
Before you start

Open the Excel workbook or Access database on Windows and make sure you can access its VBA editor. Check that the field used to assign records contains Windows login names.

Solution 1Recommended

Retrieve the Windows login name with VBA Environ

Use the UserName environment variable to capture the login name in an Excel or Access VBA procedure.

1
Open the VBA editor

In Excel or Access, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module to create a standard module.

2
Capture and test the name

Add this procedure to the module: Sub CheckWindowsUser() Dim TheUser As String TheUser = Environ("UserName") Debug.Print TheUser End Sub Place the cursor inside the procedure and press F5. Press Ctrl+G to view the result in the Immediate window.

3
Use the value in a filter

In Access, use TheUser in a report filter. For example, after assigning TheUser, call DoCmd.OpenReport "ReportName", acViewPreview, , "[AssignedTo] = '" & Replace(TheUser, "'", "''") & "'". Replace ReportName and AssignedTo with your actual report and text field names. In Excel, use TheUser as the criterion when filtering the column containing assigned login names.

Retrieve the Windows login name with VBA Environ
Do not use this as an access-control check: Environment variables can be changed, so Environ("UserName") is useful for convenience filtering but should not be relied on to secure sensitive records.
Free Microsoft Office alternative

Work with user-assigned spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for opening and editing spreadsheets in a familiar interface. Its Microsoft Office format support makes it easy to move existing files into WPS; it does not replace Microsoft Access or resolve Access VBA issues.

Filter spreadsheet records by an assigned user nameOpen and edit Microsoft Excel XLSX filesUse a familiar spreadsheet interfaceMove existing Office documents into a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

What does Environ("UserName") return?

It returns the value of the Windows UserName environment variable, typically the current login name. It does not necessarily return the user's email address or full display name.

How can I check the returned user name?

In the VBA editor, run a procedure containing Debug.Print Environ("UserName"), then press Ctrl+G to see the result in the Immediate window.

Can I use the login name to filter an Access report?

Yes. Pass a matching expression as the WhereCondition argument of DoCmd.OpenReport. Use the actual report and assigned-user field names, and escape apostrophes in the login name.

Is a VBA username filter enough to protect confidential records?

No. The UserName environment variable can be changed, and a report filter alone is not a security boundary. Use appropriate authentication and data-access controls for sensitive records.