Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Putting an X in a Clicked Cell.

Putting an X in a Clicked Cell

by Allen Wyatt
(last updated July 15, 2017)

2

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 (11681) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Putting an X in a Clicked Cell.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing the Height of a Font

Scaling the width of a font is easy to do with Word's formatting capabilities. Scaling the height of the fonts is not so ...

Discover More

Locking the Background Color

You can spend a lot of time getting the formatting in your worksheets just right. If you want to protect an element of that ...

Discover More

Changing Input Conventions

Different cultures have different conventions for displaying numbers and for parameters in Excel's worksheet functions. ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Delimited Text-to-Columns in a Macro

The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...

Discover More

Limiting Scroll Area

If you need to limit the cells that are accessible by the user of a worksheet, VBA can come to the rescue. This doesn't ...

Discover More

Mouse Click Event in VBA

Need to know if a particular cell is clicked with the mouse? Excel has no particular event handler for clicking in this way, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 3 + 8?

2017-10-17 10:54:00

Dennis Costello

Reading between the lines, it seems likely that Wendy would want the behavior of a check-box on a form - so the "X" isn't really the character X but diagonal lines spanning the entire cell. And as it turns out, Excel can do that ... Niefer was headed there with a macro that seems to have been designed to implement the behavior of a Radio Box (only one cell has the X; clicking on a different one would clear the X from all the other cells in MyRange). The next step would be to implement a Conditional Format such that if the value is 0 (in the case of Niefer's macro) or blank (for Allen's), the cell has a neutral format, while if it contains 1 or "X" the cell is formatted with diagonal border lines.

Now some questions:
- Why did Allen include the "Set rInt = Nothing" and "Set rCell = Nothing" lines in his double-click macro? Once the macro completes, don't these pointer variables disappear back into the heap? What danger could there be in leaving them alone?
- What is the effect of the "Cancel = True"? I'm guessing that it prevents the handler from being called again to handle anything that the handler itself does that Excel would interpret as another double-click; is this here just because it's a good practice, or is there something in this macro that would trigger this event? What is that? Does Niefer's "Application.EnableEvents = False" and then True accomplish the same purpose?
- Allen cycles through multiple cells in the intersection range. This seems reasonable in most event handlers - but help me understand how you could trigger a double-click event for more than one cell. It's not like you could do down-drag-release twice in a row in quick-enough succession that Excel interprets it as a double-click. So it seems to me that a double-click would always be for exactly one cell.
- Why did Niefer include "Selection.FormulaR1C1 = 0"? What is the value of Selection here (there's no ".Select" method applied prior to that spot in the macro) - is it perhaps the selection that was active before the event that triggered the SelectionChange macro? Is this how the macro is meant to implement Radio Box behavior?
- Allen's SelectionChange macro did not have a Target.Select statement, while Niefer's did - is one missing, is the other superfluous, or is this somehow wrapped up in Niefer's Application.EnableEvents = False?


2017-07-15 07:26:56

Niefer

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("MyRange")) Is Nothing Then
Application.Goto Reference:="MyRange"
Selection.FormulaR1C1 = "0"
Target.Value = "1"
End If
End If
Target.Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.