Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Highlight active cell and de-highlight previous cell

I want to highlight the current cell with a color say gray and set the color
of the last cell I was at back to what it currently was. The code below does
not work. It sets the previous cell color to white and if the cell color was
something other than white, say green, I lose the color green for the
previous cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range

'Set backcolor to whatever, change as needed
Target.Interior.ColorIndex = 15 'light gray

OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target

End Sub

--
Dave B.
  #2   Report Post  
Posted to microsoft.public.excel.programming
Art Art is offline
external usenet poster
 
Posts: 587
Default Highlight active cell and de-highlight previous cell

The problem is that you've got to store the previous ColorIndex. This
creates a new problem if you change the color of the cell you're on. Try the
following, based on your code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Static OldRange As Range
Static OldIndex As Integer
Const mColor = 15
If OldRange.Interior.ColorIndex = mColor Then
OldRange.Interior.ColorIndex = OldIndex
End If
OldIndex = Target.Interior.ColorIndex
Set OldRange = Target
Target.Interior.ColorIndex = mColor
End Sub


"dmbuso" wrote:

I want to highlight the current cell with a color say gray and set the color
of the last cell I was at back to what it currently was. The code below does
not work. It sets the previous cell color to white and if the cell color was
something other than white, say green, I lose the color green for the
previous cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range

'Set backcolor to whatever, change as needed
Target.Interior.ColorIndex = 15 'light gray

OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target

End Sub

--
Dave B.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Highlight active cell and de-highlight previous cell

Thank you, it worked great.
--
Dave B.


"Art" wrote:

The problem is that you've got to store the previous ColorIndex. This
creates a new problem if you change the color of the cell you're on. Try the
following, based on your code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Static OldRange As Range
Static OldIndex As Integer
Const mColor = 15
If OldRange.Interior.ColorIndex = mColor Then
OldRange.Interior.ColorIndex = OldIndex
End If
OldIndex = Target.Interior.ColorIndex
Set OldRange = Target
Target.Interior.ColorIndex = mColor
End Sub


"dmbuso" wrote:

I want to highlight the current cell with a color say gray and set the color
of the last cell I was at back to what it currently was. The code below does
not work. It sets the previous cell color to white and if the cell color was
something other than white, say green, I lose the color green for the
previous cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range

'Set backcolor to whatever, change as needed
Target.Interior.ColorIndex = 15 'light gray

OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target

End Sub

--
Dave B.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Highlight active cell and de-highlight previous cell

'McCurdy.Here is something inspired by Don Guillett.

Private Sub Worksheet_selectionChange(ByVal Target As Range)
Dim MyRng As Range
Set MyRng = Target '.EntireRow
Application.EnableEvents = False
On Error GoTo end1
Application.Cells.FormatConditions.Delete
With MyRng
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ROW()=ROW(INDIRECT(CELL(""address"")))"
'========
'if you want to do fonts too
' With .FormatConditions(1).Font
' .Bold = True
' .Italic = False
' .ColorIndex = 1
' End With
'==========
..FormatConditions(1).Interior.ColorIndex = 36
End With
end1:
Application.EnableEvents = True
End Sub

--
Don Guillett
SalesAid Software

"dmbuso" wrote in message
...
I want to highlight the current cell with a color say gray and set the
color
of the last cell I was at back to what it currently was. The code below
does
not work. It sets the previous cell color to white and if the cell color
was
something other than white, say green, I lose the color green for the
previous cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range

'Set backcolor to whatever, change as needed
Target.Interior.ColorIndex = 15 'light gray

OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target

End Sub

--
Dave B.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Row select mode to highlight active row of active cell Bart Fay[_2_] Excel Discussion (Misc queries) 0 May 11th 10 09:34 PM
Active Cell Highlight Bill_17256 Excel Discussion (Misc queries) 6 April 24th 09 11:00 PM
Active cell highlight E Excel Discussion (Misc queries) 21 February 15th 08 09:40 PM
How do I highlight the active cell in a spreadsheet? CW Excel Programming 2 March 9th 06 11:26 AM
Highlight Active Cell Db1712 Excel Discussion (Misc queries) 1 November 26th 04 01:14 PM


All times are GMT +1. The time now is 08:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"