CommandBars.OnUpdate
can be used to handle most custom events. In the ThisWorkbook
object:
Private WithEvents bars As CommandBars, color As Double
Private Sub bars_OnUpdate()
'If Not ActiveSheet Is Sheet1 Then Exit Sub ' optional to ignore other sheets
If ActiveCell.Interior.color = color Then Exit Sub ' optional to ignore if same color
color = Selection.Interior.color
'Debug.Print Selection.Address(0, 0), Hex(color)
If color = vbGreen Then Selection = 100 Else _
If color = vbRed Then Selection = 75
End Sub
Private Sub Workbook_Activate()
Set bars = Application.CommandBars ' to set the bars_OnUpdate event hook
End Sub
Private Sub Workbook_Deactivate()
Set bars = Nothing ' optional to unset the bars_OnUpdate event hook
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
color = Selection.Interior.color ' optional to ignore selection change events
End Sub
The above sample doesn't handle all edge cases, but can be adjust as needed.
For other custom events, the more specific CommandBarControl events should be used if possible:
CommandBarButton.Click
CommandBarComboBox.Change
CommandBarControl.OnAction
CommandBarPopup.OnAction
SelectionChange
event) or (2) you implement your own event handler. The latter of the two would work the following way: when the Excel file is opened you start a sub which is scheduled to run every x seconds with Application.OnTime. This sub scans the sheet&range you wish to monitor for any changes and then applies the designated color code – Ralph