Changing cell colour when cell selected/de-selected by mouse/arrow keys

  • Thread starter Thread starter Joe Hannett
  • Start date Start date
J

Joe Hannett

Newbie. Is it possible to get excel to change the colour
of a cell when it is selected by mouse pointer, or arrow
keys, from default white to yellow then revert to white
again when the next cell is selected?
 
Here's one way

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.FormatConditions.Delete
With Target
.FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
With .FormatConditions(1)
With .Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 5
End With
With .Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 5
End With
End With
.FormatConditions(1).Interior.ColorIndex = 6
End With

End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
You could use an event macro


Dim OLDCOLOR As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (OLDCOLOR Is Nothing) Then OLDCOLOR.Interior.ColorIndex = 0
Target.Interior.ColorIndex = 6
Set OLDCOLOR = Target

End Sub
 
Mr Bob Phillips,

Sir you are a gentleman and a scholar. It worked like a charm. Thank
you from my - nearsighted - manager and I! Your help is truly
appreciated.

Joe Hannett.
 
Thank you for this info Mr Kabel.

As my manager (for whom I have been trying to figure this out for
months) was unaware of even the UNDO facility (!) I reckon this won't
be a problem.

Kind regards
Joe Hannett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top