How to Create an Excel VBA Macro Button to Insert a Timestamp and Delete Itself
Question details
The user needs to assign a single VBA macro to multiple Form Control buttons so that clicking any button records the current timestamp in its corresponding row and immediately removes the clicked button.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Tracking multiple runners' times where each runner has a dedicated button on their row. Clicking the button marks the finish time and prevents duplicate clicks by deleting the button.
- Observed behavior
- The goal is to dynamically insert the formatted current time ("hh:mm:ss") relative to the clicked button and permanently delete the specific calling button without affecting the others.
Ensure that the Developer tab is enabled in your spreadsheet ribbon and that macro security settings allow VBA scripts to run. Save your workbook as a Macro-Enabled Workbook (.xlsm) to prevent losing your VBA code.
Use Application.Caller to Insert Timestamp and Delete the Button
By utilizing the Application.Caller property, a single VBA macro can identify exactly which Form Control button was clicked, record the time relative to that button's position, and delete it seamlessly.
This solution relies on assigning the exact same macro to all the buttons on your sheet. The `Application.Caller` function detects the name of the button that triggered the script, allowing the code to isolate the target cell on that specific row.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. Go to the top menu, click 'Insert', and select 'Module' to create a new code module.
Enter the following code into the blank module window: ```vba Sub InsertTimestampAndRemoveButton() Dim btn As Button Dim targetCell As Range Set btn = ActiveSheet.Buttons(Application.Caller) Set targetCell = btn.TopLeftCell.Offset(0, 1) targetCell.Value = Format(Now, "hh:mm:ss") btn.Delete End Sub ```
Return to your worksheet. Go to the Developer tab, click 'Insert', and choose the 'Button (Form Control)' icon. Draw a button on the first runner's row.
Right-click the button you just created, select 'Assign Macro...', choose `InsertTimestampAndRemoveButton` from the list, and click OK. You can now copy and paste this button onto the other runners' rows.

Use WPS Spreadsheet to Run VBA Macros Seamlessly
WPS Office provides robust built-in support for VBA macros, allowing you to easily implement timestamp buttons, automate repetitive tasks, and manage form controls with full compatibility with Excel macro scripts.
- 1. Download and Install WPS Office: Visit the official WPS website and install WPS Office on your Windows or Mac device.
- 2. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the runner tracking data and scripts.
- 3. Enable Developer Tools: Navigate to the Developer tab to access the VBA editor, insert Form Control buttons, and securely run your automated timestamp macros.

Frequently Asked Questions
Why does Application.Caller return an error when I test my code?
`Application.Caller` only works correctly when the macro is triggered directly by clicking the object (like a Form Control button) on the sheet. If you try to run the macro manually from the VBA editor by pressing F5, it will result in an error because there is no calling shape to reference.
Can I use ActiveX Command Buttons instead of Form Control buttons?
This specific VBA script is designed for Form Control buttons. ActiveX buttons have their own specific Click event procedures (e.g., `CommandButton1_Click()`) tied to the sheet's code module and do not support `Application.Caller` in the same way.
How do I format the timestamp to include milliseconds?
You can easily modify the format string in the code. Change `Format(Now, "hh:mm:ss")` to `Format(Now, "hh:mm:ss.000")`. Keep in mind that standard Excel VBA relies on the system clock, so precision beyond one second may require Windows API timer calls for exactness.
How do I reset the sheet after the buttons are deleted?
Because the buttons delete themselves permanently from the worksheet, you will need a separate setup macro that dynamically recreates the buttons on the desired rows. Alternatively, you can keep a clean, hidden 'Template' sheet that you duplicate for each new race.




