Macro that runs when a cell is selected

  • Thread starter Daniel Bonallack
  • Start date
D

Daniel Bonallack

I have a macro that I want to run when the user selects a cell. Can someone
give me the code that would do this?

Thanks in advance!

Daniel
 
J

Jim Thomlinson

This runs when A1 is selected. Right click the sheet tab and select view
code. Paste the following...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
MsgBox "Tada"
End If
End Sub
 
D

Dave Peterson

You can use a worksheet event:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Me.Range("A1:B3")) Is Nothing Then
Exit Sub
End If

Call MacroNameHere

End Sub

This checks to see if you selected any cell(s) in A1:B3. If you did, then it
calls MacroNameHere.

Rightclick on the worksheet tab that should have this behavior. Select view
code. Paste this code into the code window.

Change the range to check to what you want and change the name of the macro,
too.
 
D

Daniel Bonallack

Thank you Jim and Dave!
regards
Daniel

Dave Peterson said:
You can use a worksheet event:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Me.Range("A1:B3")) Is Nothing Then
Exit Sub
End If

Call MacroNameHere

End Sub

This checks to see if you selected any cell(s) in A1:B3. If you did, then it
calls MacroNameHere.

Rightclick on the worksheet tab that should have this behavior. Select view
code. Paste this code into the code window.

Change the range to check to what you want and change the name of the macro,
too.
 
Top