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.
Written by Allen Wyatt (last updated February 14, 2022)
This tip applies to Excel 2007, 2010, 2013, and 2016
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:
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.
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!
Workbooks get corrupted from time to time; that's a fact of life in an Excel world. If those corrupted workbooks contain ...
Discover MoreWhen developing macros, you can create subroutines. This is a great way to reuse common code and make your programming ...
Discover MoreRandom values are often needed when working with certain types of data. When you need to generate a random value in a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-02-05 05:33:41
Willy Vanhaelen
In the second macro it makes no sense to use a loop to cope with multiple cell ranges. Try it, select a range and when you double click on any cell in the range, the range disappears and the clicked cell becomes the active cell. So the macro can be drastically simplified from 11 to 3 lines code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("C3:P312")) Is Nothing Then Exit Sub
Target.Value = "x"
Cancel = True
End Sub
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
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments