Vba experts: Executing a macro when certain cells are selected

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Is there away to execute a macro after selecting a few certain cells???
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Yes, you can use the Worksheet_SelectionChange event procedure in the module for the worksheet? Which cells do you want to use and what macro do you want to run?
 
Upvote 0
You may want to set a flag to true as soon as the pointer enters the Worksheet_SelectionChange event and set it to false when your macro has completed doing its thing.

In a module enter
Public
fRecursive as Boolean

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Target.Address = "$J$15,$K$15:$K$16,$L$20" Then
fRecursive = True
' Rest of code here or call another sub


End If
fRecursive = False
End Sub
 
Upvote 0
You may want to set a flag to true as soon as the pointer enters the Worksheet_SelectionChange event and set it to false when your macro has completed doing its thing.

In a module enter
Public
fRecursive as Boolean

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Target.Address = "$J$15,$K$15:$K$16,$L$20" Then
fRecursive = True
' Rest of code here or call another sub


End If
fRecursive = False
End Sub

What is the purpose of that boolean variable? Nowhere do you test whether it is True of False.
 
Upvote 0
I would like when I select A4, J10, S13. Regardless if the cells have text in them or not. Thanks macro is finalizing_by_qc
 
Last edited:
Upvote 0
Try:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$4,$J$10,$S$13" Then
        Call finalizing_by_qc
    End If
End Sub
 
Upvote 0
Yes, missed out the

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
if fRecursive then
exit sub
end if


thanks!
 
Upvote 0
Is it possible to have it were if they select say b4 instead of A4,J10,S13 it will not run unless they select them in that sequence
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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