How to Create an Excel VBA Macro to Add a Username on Double-Click
Question details
The user wants to write an Excel VBA macro that automatically inputs the current Windows username into cell F2 whenever it is double-clicked.

- Product
- Excel 365
- Device & OS
- Windows
- Scenario
- Automating data entry or tracking user signatures in a specific worksheet cell without manually typing the name.
- Observed behavior
- The goal is to bypass the standard cell editing mode upon double-click and instead populate the targeted cell with the Windows environment username.
Ensure that the Developer tab is enabled in your Excel ribbon and that you have administrative permissions to run and save macros on your device.
Use the Worksheet_BeforeDoubleClick Event
Implement a specific worksheet event rather than a standard module to trigger the username insertion when the target cell is double-clicked.
Event procedures like Worksheet_BeforeDoubleClick must be placed inside the code module of the specific sheet where you want the action to occur. Placing this code in a standard module (like Module1) will prevent it from working.
Right-click the specific worksheet tab at the bottom of your Excel window and select 'View Code'. This will immediately open the VBA Editor for that exact worksheet.
In the blank code window, paste the following VBA code: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not Intersect(Target, Me.Range("F2")) Is Nothing Then Cancel = True: Target.Value = Environ("USERNAME") End If End Sub.
Close the VBA Editor to return to your Excel workbook. Double-click cell F2. Your Windows username should immediately appear in the cell, and the default editing cursor will not be activated.
Go to File > Save As. Change the file type to 'Excel Macro-Enabled Workbook (*.xlsm)' to ensure your newly added VBA code is preserved when you close the file.

Create and Run VBA Macros Easily with WPS Office
WPS Spreadsheet provides excellent compatibility with Microsoft Excel VBA macros. You can easily write, edit, and run your double-click username scripts just as you would in Excel.
- 1. Access the VBA Editor in WPS: Open your workbook in WPS Spreadsheet, navigate to the Tools tab, and click on 'Developer' or 'Macro' to launch the VBA Editor.
- 2. Insert the Event Code: Double-click your target worksheet in the Project Explorer panel and paste the Worksheet_BeforeDoubleClick code into the module.
- 3. Save and Execute: Save the file as a Macro-Enabled Workbook (.xlsm). Return to the sheet and double-click F2 to automatically insert your username.

Frequently Asked Questions
Why doesn't my macro work when I double-click cell F2?
This usually happens if the code was placed in a standard module (like Module1) instead of the specific Worksheet module. Ensure you right-clicked the sheet tab and selected 'View Code' to paste it. Additionally, verify that macros are enabled in your Trust Center settings.
Can I change the target cell from F2 to a larger range of cells?
Yes, you can modify the 'Me.Range("F2")' portion of the code to include a larger range, such as 'Me.Range("F2:F10")' or 'Me.Range("A1:C10")'. This allows the macro to trigger on any cell double-clicked within that specified area.
Will this Environ("USERNAME") macro work on a Mac?
The 'Environ("USERNAME")' function relies on Windows environment variables and is specific to Windows environments. On Excel for Mac, you would need to use the 'Application.UserName' property or an AppleScript, though results may vary based on system configurations.
How do I save the file so the macro isn't lost?
You must save the file as a Macro-Enabled Workbook. Go to File > Save As, and choose 'Excel Macro-Enabled Workbook (*.xlsm)' from the format dropdown list. Saving as a standard '.xlsx' will strip all VBA code from the file.




