Trigger Macro By Slecting Cell

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I know how to trigger a macro when a Cell's value changes, but can a macro be triggered simply by selection of a cell? If so, what is the code?

Thanks,

MikeG
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Just change the procedure name

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

use Intersect or similar to define ranges if needed, same as you would with a value change.
 
Upvote 0
Here is an example of VBA running on a particular cell change:
http://www.mrexcel.com/forum/showthread.php?t=521405

Input your code into the Worksheet Module of the Worksheet you are working with.

Right click sheet tab
Click "View Code"
Paste the code into the panel that opens. ("WorkSheet" Module)
Alt-Q to quit the VBA Editor.


Thanks - but I want the macro to trigger as soon as a cell is selected - not when the value in it is changed.

MikeG
 
Upvote 0
Just change the procedure name

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

use Intersect or similar to define ranges if needed, same as you would with a value change.

Thanks jason and datsmart. Got it running.

Mike
 
Upvote 0
The quoted link is fine. Just select your Selection Change event as suggested.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Intersect(Target, Range("D4:D79")) Is Nothing Then Exit Sub
  Application.EnableEvents = False
  If vbYes = MsgBox("Current Value: " & Target.Value & vbCrLf & _
    "Click Yes to increase to: " & Target.Value + 1, vbYesNo + vbInformation, _
    "Increase?") Then Target.Value = Target.Value + 1
  Application.EnableEvents = True
End Sub
 
Upvote 0
One follow up.

Is it possible to specify this to work if any cell in a range of cells is selected? For example, I tried this and it did not work:

If Target.Address(False, False) = "C8:C30" Then

Does it only work for one cell?

Thanks
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
'Only look at single cell changes
      If Target.Count > 1 Then Exit Sub
    'Set the range to trigger the InputBox
     Set rng = Range("C8:C30")
    'Only look at that range
    If Intersect(Target, rng) Is Nothing Then Exit Sub
         'Your code here...
End Sub
 
Upvote 0
Use Intersect.

Even my code exit if more than one cell in the intersection is selected. That can be modified if needed to iterate over the intersected range.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top