Highlight a Cell

T

Tiziano

Is there a way to conditional-format a cell with a fill color based
on whether or not another cell in the same spreadsheet is being
selected via the mouse or the up/down arrows?
In other words: Is there a way to turn say cell E1 yellow by merely
selecting (with my mouse or with the keyboard arrows) cell A1
without having to enter anything in A1?

I realize that there is a way to conditional-format E1 based on the
contents of A1 and that is not what I would like to achieve.

This is probably not possible, but I thought I'd give it a try...

Thanks.
 
B

Bernie Deitrick

Tiziano,

You can do it using the selection change event of the worksheet. Copy the
code below, then right click on the sheet tab, select "View Code" and paste
the code into the window that appears.

If you want to only turn E1 yellow when _only_ A1 is selected, comment out
the first line and uncomment the second. I wasn't sure if you wanted E1 to
turn yellow when a range like A1:C3 was selected.

HTH,
Bernie
MS Excel MVP

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1"), Target) Is Nothing Then
'If Target.Address = "$A$1" Then
Range("E1").Interior.ColorIndex = 6
Else
Range("E1").Interior.ColorIndex = xlNone
End If
End Sub
 
T

Tiziano

Thanks for your efforts, Bernie!
What if I have more than one cell that I could select and want to highlight
other
corresponding cells? For instance: If I select A1, I want to highlight
E1; if I
select A2, I want E2 to be highlighted; selecting A3 would highlight E3,
etc.
Is there a solution?

If anybody has a solution, please keep in mind that I know nothing about VB,
so I will need fairly detailed instructions...

Thanks in advance.
----
Tiziano


Bernie Deitrick said:
Tiziano,

You can do it using the selection change event of the worksheet. Copy the
code below, then right click on the sheet tab, select "View Code" and paste
the code into the window that appears.

If you want to only turn E1 yellow when _only_ A1 is selected, comment out
the first line and uncomment the second. I wasn't sure if you wanted E1 to
turn yellow when a range like A1:C3 was selected.

HTH,
Bernie
MS Excel MVP

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1"), Target) Is Nothing Then
'If Target.Address = "$A$1" Then
Range("E1").Interior.ColorIndex = 6
Else
Range("E1").Interior.ColorIndex = xlNone
End If
End Sub
 
B

Bernie Deitrick

Tiziano,

You could do this, some instructions for installing it: Copy the code, then
right click on the sheet tab, select "View Code" and paste the code into the
window that appears.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A:A"), Target) Is Nothing Then
Range("E:E").Interior.ColorIndex = xlNone
Range("E"& Target.Row).Interior.ColorIndex = 6
End If
End Sub

HTH,
Bernie
MS Excel MVP

Tiziano said:
Thanks for your efforts, Bernie!
What if I have more than one cell that I could select and want to highlight
other
corresponding cells? For instance: If I select A1, I want to highlight
E1; if I
select A2, I want E2 to be highlighted; selecting A3 would highlight E3,
etc.
Is there a solution?

If anybody has a solution, please keep in mind that I know nothing about VB,
so I will need fairly detailed instructions...

Thanks in advance.
 
Top