In Excel VBA on Windows, use Environ$("USERNAME") to read the current Windows account name. Use Application.UserName instead if you want the name configured in Excel; it is editable and is not proof of a person’s identity.
Two different meanings of “username”
“Get the username” can mean at least two different things in an Excel macro. The Windows account name is supplied by the operating system environment. Excel’s user name is an Office application setting used as a display name in certain features. They may match, but there is no guarantee that they do.
For example, a Windows sign-in might be jsmith, while Excel’s configured user name is “Jordan Smith.” A company may also configure Excel with a department name, or a person may leave the default value unchanged. Choose the source based on what the macro actually needs, rather than treating the two values as interchangeable.
- Windows account: best for displaying or recording the current Windows sign-in name in a workbook.
- Excel user name: best when the macro needs the friendly name configured in Excel.
- Verified identity: neither value is sufficient on its own for access control, approvals, or a secure audit trail.
Read the Windows account with Environ
For a typical Microsoft 365 installation on Windows, VBA’s Environ function is the simplest option. The dollar-sign form, Environ$, returns a string. The name of the environment variable is USERNAME; the function name is not a special Excel worksheet formula.
Sub ShowWindowsUsername()
Dim windowsUser As String
' Read the current Windows username environment variable.
windowsUser = Environ$("USERNAME")
If Len(windowsUser) = 0 Then
MsgBox "The USERNAME environment variable is not available."
Exit Sub
End If
MsgBox windowsUser, vbInformation, "Windows account"
End Sub
To try it, open the workbook in desktop Excel, press Alt+F11 on Windows to open the Visual Basic Editor, choose Insert > Module, and paste the procedure into the module. Run it with the cursor inside the procedure and press F5, or return to Excel and run it from the Macro dialog. On Mac, the Visual Basic Editor can be opened with Option+F11 (some keyboards require Fn as well); the available shortcuts can depend on the keyboard and macOS settings.
Before running any macro, save a copy of the workbook. Macros can change or replace workbook data, and their actions cannot be reversed with Ctrl+Z. This sample only displays a value, but that is a useful habit for any VBA procedure.
Read Excel’s configured user name
If the intended value is the name set inside Excel, read the Application.UserName property. It is a string property of the Excel application object and does not require a worksheet or workbook reference.
Sub ShowExcelUsername()
Dim excelUser As String
' Read the name configured in Excel, not a verified sign-in identity.
excelUser = Application.UserName
MsgBox excelUser, vbInformation, "Excel user name"
End Sub
In Excel for Microsoft 365 on Windows, the setting is at File > Options > General, under the section for personalizing the copy of Microsoft Office. The exact wording and placement can vary across Office builds. Excel for Mac has its own settings or preferences for user information; it does not use the Windows File > Options path. Because this setting is designed as user information, users can change it, and it may not correspond to the account currently signed in to Windows or Microsoft 365.
VBA can also assign a value to Application.UserName. That changes the application’s configured display name; it does not rename the Windows account or authenticate the person running the macro. Avoid changing it merely to make a log appear more trustworthy.
Which method should you choose?
Decide what the value will be used for before writing the macro. For a label such as “Report prepared by,” a friendly Excel name may be more readable. For troubleshooting or a simple local record of the Windows sign-in, the environment variable may be more relevant. If the workbook will be used on multiple operating systems, make the platform choice explicit and test it in the intended environment.
| Need | Likely choice | Important limitation |
|---|---|---|
| Show the Windows sign-in name | Environ$("USERNAME") |
Depends on the operating system environment variable being present and accurate. |
| Show a familiar name configured in Excel | Application.UserName |
Editable and not an authenticated identity. |
| Control access or prove who approved a record | A suitable authenticated system, not either value alone | A workbook macro is not a secure identity service. |
A practical example is a shared reporting workbook. If it displays the Windows account in a status message to help users identify which profile ran a report, Environ$("USERNAME") is a reasonable convenience. If the report is meant to show a person’s full name to readers, Application.UserName may be more presentable, but the workbook should not imply that Excel verified the name.
Windows, Mac, and Excel for the web
This guidance assumes desktop Excel for Microsoft 365 on Windows. Environment variable names are operating-system-specific. In a Mac environment, USERNAME may not be defined; the conventional account variable is often USER, but environment configuration can differ. Consequently, do not assume that a Windows macro using Environ$("USERNAME") will return a useful value on every Mac. Check for an empty result and test the specific Mac setup. Application.UserName is generally the more portable way to read Excel’s configured display name, while retaining the same identity limitation.
Excel for the web does not run VBA macros. A workbook containing VBA can be stored or opened in a browser, but the VBA procedure must be run in a supported desktop version of Excel. If browser users need a current user identity, use an appropriate web-based workflow or authenticated service rather than expecting a VBA macro to run in the browser.
Common problems and troubleshooting
- The result is blank: the requested environment variable may not be defined in that session or platform. Check the string’s length before using it, as in the example, and provide a useful fallback rather than silently writing an empty value.
- The result is not a full name:
USERNAMEcommonly contains an account identifier, not a person’s display name. Windows does not promise that it will be formatted as first and last name. - The value seems stale or unexpected: the macro reads the environment available to the Excel process when it runs. If the user changes accounts, launches Excel in a different context, or uses a managed environment, verify the value in that actual session.
- The macro will not run: confirm that the workbook is open in desktop Excel, that the code is in a standard module, and that macro security permits the file to run. Do not enable macros in an untrusted workbook just to inspect a username.
- The workbook is being edited by several people: a value written into a cell is only a snapshot from the time the macro ran. It does not update automatically when another person opens the workbook, and simultaneous editing can create additional recordkeeping issues.
For a quick diagnostic, temporarily display both values in one message box and compare them. If they differ, that is not necessarily an error: the two values come from different settings and serve different purposes. Remove diagnostic code that exposes personal or account information if it is not needed in the finished workbook.
Privacy, reliability, and audit records
A username can be personal information. Before placing it in a shared workbook, consider who can see the file, whether the value needs to be retained, and whether a less identifying label would do. A macro that writes a name into a cell can also leave it in version history, copies, exports, or backups, depending on how the file is stored and managed.
Neither Environ$("USERNAME") nor Application.UserName should be used as a password, permission check, or dependable proof of approval. The Excel property is user-editable, and environment variables are not a secure authentication mechanism. A user can alter workbook code or data as well. For a regulated or consequential audit trail, use an organization-approved system that records authenticated users and protected, time-stamped events; ask the relevant IT or compliance team what is appropriate.
Keep the captured value’s meaning clear. A column titled “Windows account when macro ran” is more precise than “Approved by,” unless a separate approval process actually verifies the approver. Document whether the value is a display label, an account identifier, or merely a convenience for troubleshooting.
Alternatives when Environ is not enough
On Windows, the Windows API includes a GetUserName function that can retrieve the account name for the current security context. VBA can call it through a declared API procedure, but that approach adds complexity: the declaration must be compatible with the VBA version and 32-bit or 64-bit Office, and the returned value still does not by itself establish a person’s full name or permission. Use it only when there is a concrete reason to avoid the environment variable and someone can maintain the API declaration.
For ordinary workbooks, first consider whether the user should choose a name from a controlled list or enter one explicitly. That can make a report clearer, but a manually entered value is still not verified identity. For a multi-user process where identity matters, prefer an authenticated application or service designed for that purpose. The right alternative depends on whether the requirement is convenience, portability, or security; those are different problems.
Frequently Asked Questions
What is the simplest VBA code to get the username?
On Windows, assign Environ$("USERNAME") to a declared string variable. It returns the current process’s Windows username environment value, which is usually an account name rather than a full display name.
Is Application.UserName the same as the Windows username?
No. Application.UserName reads Excel’s configured user name, while Environ$("USERNAME") reads a Windows environment variable. The values can match, but they can also differ.
Can I use the username in a worksheet formula?
Not with these VBA expressions directly: they are VBA code, not worksheet functions. A macro can write a value into a cell, but that value is a snapshot and will not automatically change when another person opens the workbook.
Will Environ$("USERNAME") work in Excel for Mac?
Do not rely on that Windows variable name on Mac. The available environment variables can differ; USER is commonly used for a Mac account name, but the setup should be checked. Excel for the web does not execute VBA at all.
Can I use either value to secure a workbook or record approvals?
No. Both are useful as labels or diagnostics, but neither verifies a person’s identity. Use an authenticated system with appropriate access controls and audit features for security-sensitive approvals.
Related
- How to Insert a Line Above a Letter in Microsoft Word
- How to Insert a Line Above a Table in Word
- How to Insert a Functioning Checkbox in Microsoft Word
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Word VBA: Add a Text Watermark with a MacroYes. In Word for Microsoft 365 on Windows, you can use VBA to add a text watermark by…
- Easy Macro Recorder in Excel: How to Record and Use OneYes. Microsoft Excel for Microsoft 365 includes a built-in macro recorder: choose Developer > Record Macro, perform the…
- VBA Loop Types: For, Do, and While ExplainedVBA has four commonly encountered loop constructs: For…Next, For Each…Next, Do…Loop, and While…Wend. Use For…Next when a counter…
- Excel VBA: Get the Current Workbook File Path EasilyIn Excel VBA, use ThisWorkbook.Path to get the folder path of the workbook that contains the code, or…
- VBA Range.Resize: Change a Range’s Dimensions in ExcelIn Excel VBA, use the Resize property to return a range with a different number of rows or…
- Excel VBA UserForm Data Entry: Build a Simple FormTo enter data through an Excel VBA UserForm, create a form with text boxes and a Save button,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.