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)

5

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

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Displaying a Message in the Status Bar

A great place for your macro to display status information is, well, in the status bar. Displaying the information is ...

Discover More

Setting Numerator Height Spacing in the Equation Editor

The Equation Editor is a great way to add an professional-looking equation to your document. It allows you to modify many ...

Discover More

Determining a Random Value

Random values are often needed when working with certain types of data. When you need to generate a random value in a ...

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)

Replacing and Converting in a Macro

When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially ...

Discover More

Calculating Only the Active Workbook

When you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...

Discover More

Documenting Changes in VBA Code

Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your ...

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 two more than 2?

2018-12-08 17:06:45

Robert

Peter,

thank you very much!

Best regards,
Roibert


2018-12-04 19:38:07

peter Atherton

Robert

I have editied the first sub so that it will only enter one "x" into a column. You can change the range iwhen eting the range e.g.
Set rInt = Intersect(Target, Range("C3:P312")) to
Set rInt = Intersect(Target, Range("C3:P20, H3:K45"))

the code with same range is

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rInt As Range
Dim rCell As Range
Dim rColumn As Range
Dim counter As Integer

Set rInt = Intersect(Target, Range("C3:P312"))

If Not rInt Is Nothing Then
Set rColumn = Range(Cells(3, rInt.Column), Cells(312, rInt.Column))

counter = WorksheetFunction.CountIf(rColumn, "=x")
If counter > 0 Then
Exit Sub
End If
For Each rCell In rInt
rCell.Value = "x"
Next
End If

End Sub


2018-12-03 18:30:16

Robert

Hi,
please, can you add a few lines into Niefer's code which will enable to use several ranges with the same principle within each range (no more than one x in one range)?

I will appreciate your help!
How can I find your answer? Can you e-mail it to me or you will post it in this thread?

Best regards!
Robert


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.