View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Script Not working

You can prevent the background color and borders destruction by storing the
relevant properties in their own Static variables and resetting them before
updating the OldCell properties. Here is your code modified to do that...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldCell As Range
Static OldInterior As Long
Static OldBorders As Long
If Application.CutCopyMode = 0 Then
ActiveSheet.Unprotect Password:="justme"
If Not OldCell Is Nothing Then
OldCell.Interior.ColorIndex = OldInterior
OldCell.Borders.LineStyle = OldBorders
End If
Set OldCell = Target
OldInterior = Target.Interior.ColorIndex
OldBorders = Target.Borders.LineStyle
OldCell.Interior.ColorIndex = 6
OldCell.Borders.LineStyle = xlContinuous
Else
If OldCell Is Nothing Then
Set OldCell = Target
Else
Set OldCell = Union(OldCell, Target)
End If
End If
ActiveSheet.Protect Password:="justme"
End Sub

Also, you might want to add this line...

If Target.Count < 1 Then Exit Sub

at the beginning of the code to prevent the error that occurs when multiple
cells are selected (unless you think it necessary to expand the code to
handle coloring and re-coloring multiple cells, which would complicate
things a little).

--
Rick (MVP - Excel)


"Gord Dibben" <gorddibbATshawDOTca wrote in message
...
I recommend this for a protected sheet but there is still some
destruction(see below)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldCell As Range
If Application.CutCopyMode = 0 Then
ActiveSheet.Unprotect Password:="justme"
If Not OldCell Is Nothing Then
OldCell.Interior.ColorIndex = xlColorIndexNone
OldCell.Borders.LineStyle = xlLineStyleNone
End If
Set OldCell = Target
OldCell.Interior.ColorIndex = 6
OldCell.Borders.LineStyle = xlContinuous
Else
If OldCell Is Nothing Then
Set OldCell = Target
Else
Set OldCell = Union(OldCell, Target)
End If
End If
ActiveSheet.Protect Password:="justme"
End Sub

Will color the activecell yellow.

Note: will wipe out existing background color of activecell cell unless
BG
color is due to CF


Gord

On Sat, 6 Sep 2008 13:08:24 -0400, "Rick Rothstein"
wrote:

My guess is that the code the OP is currently using won't run on a
protected
sheet either, so I would think the RowLiner Add-In solution should still
be
a viable choice.