Advertisement
inmyhand123

VBA - 2025 - focus cell - chat GPT recommended

Feb 13th, 2025
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.18 KB | None | 0 0
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.     Static prevRow As Integer, prevCol As Integer
  3.     Static lastLeftRow As Integer ' Track last highlighted row in A:C
  4.     Static wasOutside As Boolean  ' Track if last selection was outside main range
  5.  
  6.     Dim mainRange As Range, leftRange As Range, fullRange As Range
  7.     Dim rowRange As Range, affectedCells As Range, leftRowRange As Range
  8.  
  9.     ' Define key ranges
  10.     Set mainRange = Me.Range("D16:AL74") ' Main range for row & column highlighting
  11.     Set leftRange = Me.Range("A16:C74")  ' Left-side range (A:C) for clearing when needed
  12.     Set fullRange = Me.Range("A16:AL74") ' Full range for clearing old highlights
  13.  
  14.     ' If selecting the same cell again, exit immediately
  15.     If Target.Row = prevRow And Target.Column = prevCol Then Exit Sub
  16.  
  17.     ' Disable UI updates for maximum performance
  18.     Application.ScreenUpdating = False
  19.     Application.EnableEvents = False
  20.     Application.Calculation = xlCalculationManual
  21.  
  22.     ' If selecting outside the main range
  23.     If Intersect(mainRange, Target) Is Nothing Then
  24.         If Not wasOutside Then
  25.             fullRange.Interior.ColorIndex = xlColorIndexNone ' Clear only once when leaving
  26.             wasOutside = True ' Mark that we are outside
  27.         End If
  28.         prevRow = 0
  29.         prevCol = 0
  30.         lastLeftRow = 0 ' Reset left-side tracking
  31.         GoTo RestoreSettings
  32.     End If
  33.  
  34.     ' Reset outside flag since we are now inside main range
  35.     wasOutside = False
  36.  
  37.     ' Clear previous row & column highlights **only if they were changed**
  38.     If prevRow > 0 And prevRow <> Target.Row Then
  39.         Set rowRange = Intersect(Me.Rows(prevRow), fullRange)
  40.         If Not rowRange Is Nothing Then rowRange.Interior.ColorIndex = xlColorIndexNone
  41.     End If
  42.     If prevCol > 0 And prevCol <> Target.Column Then
  43.         Set affectedCells = Intersect(Me.Columns(prevCol), mainRange)
  44.         If Not affectedCells Is Nothing Then affectedCells.Interior.ColorIndex = xlColorIndexNone
  45.     End If
  46.  
  47.     ' If the row changed, clear the left-side (`A16:C74`) of the previous row
  48.     If lastLeftRow > 0 And lastLeftRow <> Target.Row Then
  49.         Set leftRowRange = Intersect(Me.Rows(lastLeftRow), leftRange)
  50.         If Not leftRowRange Is Nothing Then leftRowRange.Interior.ColorIndex = xlColorIndexNone
  51.     End If
  52.  
  53.     ' Highlight the entire row within fullRange (A16:AL74)
  54.     Set rowRange = Intersect(Me.Rows(Target.Row), fullRange)
  55.     If Not rowRange Is Nothing Then rowRange.Interior.ColorIndex = 6 ' Yellow
  56.  
  57.     ' Highlight column only within mainRange (D16:AL74)
  58.     Set affectedCells = Intersect(mainRange, Target.EntireColumn)
  59.     If Not affectedCells Is Nothing Then affectedCells.Interior.ColorIndex = 35 ' Light Blue
  60.  
  61.     ' Remove highlight from the selected cell
  62.     Target.Interior.ColorIndex = xlColorIndexNone
  63.  
  64.     ' Update previous selections
  65.     prevRow = Target.Row
  66.     prevCol = Target.Column
  67.     lastLeftRow = Target.Row ' Track last row for left-side clearing
  68.  
  69. RestoreSettings:
  70.     ' Restore application settings
  71.     Application.Calculation = xlCalculationAutomatic
  72.     Application.EnableEvents = True
  73.     Application.ScreenUpdating = True
  74. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement