Enable/Disable SelectionChange macro on-the-fly as needed

RogerC

Well-known Member
Joined
Mar 25, 2002
Messages
537
I'm using this code provided by Marcelo Branco to visually highlight a cell when it becomes the active cell by changing its font color to orange, then changing the font color back to black when the user clicks elsewhere and the cell becomes non-active.

The SelectionChange event works great for this, but sometimes it becomes a nuisance when doing other things within the sheet. I would like to be able to disable this macro through a button as needed, then turn it back on (with the same button if possible) when it's needed again.

Can this be done?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)    Static PreviousCell As Range
    
    If Not Intersect(Target, Range("H:H")) Is Nothing Then
      If IsEmpty(Target) Then Exit Sub
      If PreviousCell Is Nothing Then
          Set PreviousCell = Target
          Target.EntireColumn.Font.ColorIndex = xlAutomatic
      Else
          PreviousCell.Font.ColorIndex = xlAutomatic
      End If
      Target.Font.ColorIndex = 46
      Set PreviousCell = Target
    Else
        Columns("H:H").Font.ColorIndex = xlAutomatic
    End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
Code:
Private Sub CommandButton1_Click()
    Application.EnableEvents = Not Application.EnableEvents
End Sub
 
Upvote 0
Thank you for this suggestion, Fluff. Can you tell me where to place this code? In a Sheet Object, ThisWorkbook, or a Module?

Also, with this code assigned to a single button, will it turn on the SelectionChange macro if it is already in the off state and turn it off if it is already in the on state?
 
Upvote 0
If you use an activex button then it will need to go in the sheet module, but if you use a form control button then it needs to go into a normal module & you'll need to rename it. Like
Code:
Sub MyBtn()
    Application.EnableEvents = Not Application.EnableEvents
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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