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

Word 2007 Documents Unreadable in Earlier Word Versions

Some people report that even if they save a Word 2007 document in a format suitable for earlier versions of Word, the ...

Discover More

Setting a VBA Variable from a Bookmark

Bookmarks are quite helpful in a document. You may want to transfer the contents of a bookmark into a macro variable in ...

Discover More

Creating Charts in VBA

Most charts you create in Excel are based on information stored in a worksheet. You can also create charts based on ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More

Running a Procedure when a Workbook is Opened

Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.

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 9 + 6?

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.