You can only do this through VBA. Here is some code you can drop into the Sheet object.
Private lastRange As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
If Not lastRange Is Nothing Then
lastRange.Interior.Color = Range("A1").Interior.Color
End If
Else
Set lastRange = Target
End If
End Sub
The lastRange
object is saved from selection to selection. This is the only way to have VBA remember what range we want to highlight. Whenever you change selection, it sets that selection to lastRange
unless it's A1
in which case it colors the previous selection as whatever color A1
is..
Based on your comments, though, your need is a bit broader than this. Since you want a range of cells of various colors, this is probably the code you want:
Private lastRange As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
If Not lastRange Is Nothing Then
lastRange.Interior.Color = Target.Interior.Color
Exit Sub
End If
End If
End If
Set lastRange = Target
End Sub
Now there's a whole range of cells A1:A5
that can be all different colors. If you select a single cell in that range, it will set your previous selection to be the same color as that current cell. You can adjust the A1:A5
range to be whatever you need.
This has one oddity that I know of: If you select a range, then a shape, then A1
, the range you had selected before is still stored as lastRange
. That's probably OK but I don't know your use case.
Format painter
option? Will that work for you?