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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...Discover More
Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...Discover More
Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.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.