Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Static prevRow As Integer, prevCol As Integer
- Static lastLeftRow As Integer ' Track last highlighted row in A:C
- Static wasOutside As Boolean ' Track if last selection was outside main range
- Dim mainRange As Range, leftRange As Range, fullRange As Range
- Dim rowRange As Range, affectedCells As Range, leftRowRange As Range
- ' Define key ranges
- Set mainRange = Me.Range("D16:AL74") ' Main range for row & column highlighting
- Set leftRange = Me.Range("A16:C74") ' Left-side range (A:C) for clearing when needed
- Set fullRange = Me.Range("A16:AL74") ' Full range for clearing old highlights
- ' If selecting the same cell again, exit immediately
- If Target.Row = prevRow And Target.Column = prevCol Then Exit Sub
- ' Disable UI updates for maximum performance
- Application.ScreenUpdating = False
- Application.EnableEvents = False
- Application.Calculation = xlCalculationManual
- ' If selecting outside the main range
- If Intersect(mainRange, Target) Is Nothing Then
- If Not wasOutside Then
- fullRange.Interior.ColorIndex = xlColorIndexNone ' Clear only once when leaving
- wasOutside = True ' Mark that we are outside
- End If
- prevRow = 0
- prevCol = 0
- lastLeftRow = 0 ' Reset left-side tracking
- GoTo RestoreSettings
- End If
- ' Reset outside flag since we are now inside main range
- wasOutside = False
- ' Clear previous row & column highlights **only if they were changed**
- If prevRow > 0 And prevRow <> Target.Row Then
- Set rowRange = Intersect(Me.Rows(prevRow), fullRange)
- If Not rowRange Is Nothing Then rowRange.Interior.ColorIndex = xlColorIndexNone
- End If
- If prevCol > 0 And prevCol <> Target.Column Then
- Set affectedCells = Intersect(Me.Columns(prevCol), mainRange)
- If Not affectedCells Is Nothing Then affectedCells.Interior.ColorIndex = xlColorIndexNone
- End If
- ' If the row changed, clear the left-side (`A16:C74`) of the previous row
- If lastLeftRow > 0 And lastLeftRow <> Target.Row Then
- Set leftRowRange = Intersect(Me.Rows(lastLeftRow), leftRange)
- If Not leftRowRange Is Nothing Then leftRowRange.Interior.ColorIndex = xlColorIndexNone
- End If
- ' Highlight the entire row within fullRange (A16:AL74)
- Set rowRange = Intersect(Me.Rows(Target.Row), fullRange)
- If Not rowRange Is Nothing Then rowRange.Interior.ColorIndex = 6 ' Yellow
- ' Highlight column only within mainRange (D16:AL74)
- Set affectedCells = Intersect(mainRange, Target.EntireColumn)
- If Not affectedCells Is Nothing Then affectedCells.Interior.ColorIndex = 35 ' Light Blue
- ' Remove highlight from the selected cell
- Target.Interior.ColorIndex = xlColorIndexNone
- ' Update previous selections
- prevRow = Target.Row
- prevCol = Target.Column
- lastLeftRow = Target.Row ' Track last row for left-side clearing
- RestoreSettings:
- ' Restore application settings
- Application.Calculation = xlCalculationAutomatic
- Application.EnableEvents = True
- Application.ScreenUpdating = True
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement