logo
search
VBA & Macro Problems

How to Create an Excel VBA Macro to Add a Username on Double-Click

Olivia MillerOlivia Miller Oct 1, 2026 868 views

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.

How to Create an Excel VBA Macro to Add a Username on Double-Click
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Worksheet Code Module

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.

2
Paste the VBA Event Code

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.

3
Test the Macro

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.

4
Save as a Macro-Enabled Workbook

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.

Use the Worksheet_BeforeDoubleClick Event
Understanding the Code: Setting 'Cancel = True' in the macro ensures the default double-click behavior (entering cell edit mode) is safely bypassed, keeping the data entry seamless.
Powerful VBA Support

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. 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. 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. 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.
Fully compatible with Microsoft Excel VBA code and event proceduresSupports .xlsm and .xlsb macro-enabled formats nativelyLightweight application with a familiar ribbon interfaceFree to use for everyday complex spreadsheet tasks
microsoft office alternative - wps office

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.