Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Putting an X in a Clicked Cell.
by Allen Wyatt
(last updated February 16, 2017)
Wendy has a worksheet that has quite a bit of data in it, with the main data in the range C3:P312. She would like to have a macro that, if she clicks a cell in this data range, would put an "x" into the cell.
There is no event that Excel can recognize as a "click" on a cell. Perhaps the closest event is the SelectionChange event, which is triggered every time the cell selection changes. The event handler could then check to make sure that the cell selection is within the C3:P312 range, and then place an "x" in the cell if it is. The following event handler will do that:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rInt As Range Dim rCell As Range Set rInt = Intersect(Target, Range("C3:P312")) If Not rInt Is Nothing Then For Each rCell In rInt rCell.Value = "x" Next End If Set rInt = Nothing Set rCell = Nothing End Sub
There is a problem with this approach, however: Not only will the SelectionChange event trigger when you click on a different cell, it also triggers if you use the keyboard to move from one cell to another in the worksheet. This means that if you use the keyboard to move about the worksheet you will leave a tail of "x" characters in each cell you transit.
One way around this is to change the event that triggers the check and change of the cells. While Excel has no "click" event, there is a "double click" event. This means that you can change the cell on which you double click, as shown here:
Private Sub Worksheet_BeforeDoubleClick( _ ByVal Target As Range, Cancel As Boolean) Dim rInt As Range Dim rCell As Range Set rInt = Intersect(Target, Range("C3:P312")) If Not rInt Is Nothing Then For Each rCell In rInt rCell.Value = "x" Next End If Set rInt = Nothing Set rCell = Nothing Cancel = True End Sub
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3378) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Putting an X in a Clicked Cell.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...Discover More
When you need to get rid of characters in the middle of a cell value, the best way to do it is through the use of macros. ...Discover More
Do you have a macro that need to read and write files? If so, then there is a good chance you need to specify the default ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.