VBA & Macro Problems

Toggle an X in an Excel Cell with a Mouse Click

Ayan MasoodAyan Masood Nov 25, 2026 869 views

Question details

The user wants to toggle an X in an Excel cell by selecting it with the mouse.

Toggle an X in an Excel Cell with a Mouse Click
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet needs a clickable range where selecting a cell adds or removes an X.
Observed behavior
Selecting a cell in the specified range should add an X or clear an existing X.
Before you start

Open the workbook in desktop Excel and choose the cells to use as toggles. Use empty cells: the macro will replace any other contents in the target range with an X.

Solution 1Recommended

Add a SelectionChange macro to the worksheet

Use a worksheet event to toggle an X when a single cell in a specified range becomes selected.

1
Open the worksheet code

Right-click the worksheet tab and select View Code to open that sheet's VBA code module.

2
Paste the event macro

Paste this code into the worksheet module, replacing A1:A10 with your desired range: Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then If Target.Value = "X" Then Target.ClearContents Else Target.Value = "X" End If End If End Sub

3
Save as a macro-enabled workbook

In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). If prompted when reopening a trusted workbook, select Enable Content to allow its macros to run.

4
Test the toggle

Click a cell in the target range to add an X. Select a different cell, then click the original cell again to remove the X.

Add a SelectionChange macro to the worksheet
Selection must change: Clicking an already selected cell does not trigger SelectionChange. If the worksheet already has a Worksheet_SelectionChange procedure, incorporate the toggle logic into that procedure instead of adding a duplicate.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move existing workbooks. It does not replace the Excel-specific VBA setup described above; macro support can vary by version.

Open and edit Microsoft Excel .xlsx spreadsheetsUse familiar spreadsheet tools for working with cell rangesMove existing Office files into a lightweight suiteGet started with a free Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't clicking the same cell again remove the X?

The macro runs only when the selection changes. Click a different cell first, then click the original cell again.

How do I change which cells act as toggles?

Right-click the worksheet tab, select View Code, and replace A1:A10 in Me.Range("A1:A10") with your chosen range.

Will this macro overwrite existing cell contents?

Yes. If a target cell does not contain X, the macro replaces its contents with X. If it contains X, the macro clears the contents.

Can I run this macro in Excel for the web?

No. VBA macros do not run in Excel for the web. Open the macro-enabled workbook in desktop Excel and enable macros if you trust the file.