Change interior color of the previously selected cell

Viewed 25

I'm looking to do something simple in Excel:

  1. Select a random cell I want its interior colour changed of.
  2. Click a specific cell with the color change code attached to it.

Simply put; I want to click any cell I want, then click cell F1 and have the previously selected cell turn blue.

What I have is:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("$F$1")) Is Nothing Then
ActiveCell.Interior.Color = vbBlue
End If
End Sub

The problem here of course is that "ActiveCell" has changed by the time its run in the code. I'd need something like "PreviouslyActiveCell" but I don't think it exists and don't know how to get there another way.

Help always appreciated, thanks!

EDIT: the linked question helped me solve it. Thanks everyone! My working code is now:

Public PreviousActiveCell As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static pPrevious As Range
Set PreviousActiveCell = pPrevious
Set pPrevious = ActiveCell

If Not Intersect(Target, Range("$F$1")) Is Nothing Then
PreviousActiveCell.Interior.Color = vbBlue 
End If 
End Sub
0 Answers
Related