Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This subroutine highlights columns on a calendar in Excel based on the selected activity by leveraging the Worksheet_SelectionChange event
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Set the context appropriately using With...End With
With Sheets("calendar")
'Clear all previously-applied highlighting for easy readability
.Cells.Interior.ColorIndex = xlColorIndexNone
'Switch on the address of the selected cell
Select Case Target.Address
Case "$A$1" '<~ if cell A1 is clicked, highlight cells C5-C9 yellow
.Range("C5:C9").Interior.Color = RGB(255, 255, 0)
Case "$A$2" '<~ if cell A2 is clicked, highlight cell D5-D9 green
.Range("D5:D9").Interior.Color = RGB(0, 255, 0)
Case "$A$3" '<~ if cell A3 is clicked, highlight cell E5-E9 orange
.Range("E5:E9").Interior.Color = RGB(255, 165, 0)
End Select
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.