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)

6

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

Changing the Perspective of Your Chart

Microsoft Graph can be a handy way to add quick and dirty charts to your document. When working with 3-D charts, you can ...

Discover More

Bumping Numbers in a Document

If your documents include words that contain numbers (such as a list of parts numbers) you may need a way to increment ...

Discover More

Setting Row Height

When you enter information into a row on a worksheet, Excel automatically adjusts the height of the row based on what you ...

Discover More

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!

More ExcelTips (ribbon)

Deleting Blank Rows

Got some pesky blank rows in your data that you want to get rid of? This tip provides a wide variety of methods you can ...

Discover More

Determining the Length of a String

Macros are great for working with strings, and one of the most commonly used string functions is Len. This tip explains ...

Discover More

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

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

2019-03-31 10:31:58

Joshyy

How could ellens code be modified to clear the selection when the cell already has a "X" inside

i have modified his codes to my needs below(it works) but is there any else i should remove/change?

Private Sub Worksheet_BeforeDoubleClick( _
ByVal Target As Range, Cancel As Boolean)
Dim rInt As Range
Dim selectedCell As Range

Set rInt = Intersect(Target, Range("C:C"))
If Not rInt Is Nothing Then
For Each selectedCell In rInt
selectedCell.Value = "RECEIVED"
Next
End If

Set rInt = Intersect(Target, Range("C:C"))
If Not rInt Is Nothing Then
For Each selectedCell In rInt
selectedCell.Value = ""
Next
End If

Set rInt = Intersect(Target, Range("D:D"))
If Not rInt Is Nothing Then
For Each selectedCell In rInt
selectedCell.Value = "INSTALLED"
Next
End If
Set rInt = Nothing
Set selectedCell = Nothing
Cancel = True

End Sub


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.