PDA

View Full Version : [SOLVED:] Active cell color change



Rejje
06-03-2011, 04:38 PM
How can I make the active cell appear in a specific rgb color? If not active = whatever color format initially was chosen in design.

ntrauger
06-03-2011, 09:29 PM
Try this on for size:

Const activeFill As Long = 255 '<--Use sub 'a' below to find the right value for your color of choice.
Dim oldCell As Range
Dim oldCellFill As Long

Private Sub Worksheet_Activate()
Set oldCell = Selection
oldCellFill = Selection.Interior.Color
Selection.Interior.Color = activeFill
End Sub

Private Sub Worksheet_Deactivate()
If oldCellFill = 16777215 Then '<--Workaround for no fill
oldCell.Interior.ColorIndex = 0
Else
oldCell.Interior.Color = oldCellFill
End If
Set oldCell = Nothing
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If oldCellFill = 16777215 Then
oldCell.Interior.ColorIndex = 0
Else
oldCell.Interior.Color = oldCellFill
End If
Set oldCell = Target
oldCellFill = Target.Interior.Color
Target.Interior.Color = activeFill
End Sub

Sub a()
Debug.Print RGB(256, 0, 0)
End Sub

Rejje
06-04-2011, 04:32 AM
Hmm - I don't get it to work. Where do I place it?

I tried in pasting it into a worksheet but i get "run time error no. 91..." at "oldCell.Interior.Color = oldCellFill"

What's wrong?

ntrauger
06-04-2011, 07:07 AM
Right. Should have mentioned that if you go straight from the VBE to the spreadsheet after resetting the project by editing code or some other way, oldCell will not have an object assigned, so it will error under that circumstance. Code will work under normal operation (to see this, paste the code into one sheet while a different sheet is active, then activate the sheet you pasted the code into and click around), but I suggest commenting it out while building the workbook.

Rejje
06-06-2011, 12:47 AM
Perfect result and solved!

Thanks a lot Nate!