PDA

View Full Version : Solved: Run code on cell selection



pcsparky
04-18-2009, 07:29 AM
I know how to run code on Selection Change in a Worksheet but does anyone know how to run the code when the mouse is clicked in a cell, ie. no change in the data, just selection of the cell itself?

Simon Lloyd
04-18-2009, 07:52 AM
Use the Worksheet_Change event, to narrow it to a certain range then use the intersect method to identify it like this:Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
'do something
End If
End Subor if you have multiple rangesPrivate Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10,B1,D25:F20")) Is Nothing Then
'do something
End If
End Sub

mdmackillop
04-18-2009, 08:43 AM
I know how to run code on Selection Change in a Worksheet but does anyone know how to run the code when the mouse is clicked in a cell, ie. no change in the data, just selection of the cell itself?
That is exactly what SelectionChange does

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
MsgBox Target.Address
End If
End Sub

Simon Lloyd
04-18-2009, 12:01 PM
MD, i think the OP was asking what triggers the event on move to cell....well thats how i took it :)

mdmackillop
04-18-2009, 12:43 PM
Simon,
I think you're having what is known as a "senior moment":old:
Regards
Malcolm

Simon Lloyd
04-19-2009, 01:12 AM
Ah, yes MD :bow: , i think it must be this freakish nice weather we're having it's drying my sawdust out a little too quick! :blush