Hi John
I thought that protecting the sheet would prevent the user from pasting
a new conditional format over yours. I first unlocked all the cells,
otherwise no pasting could occur, then protected the sheet, however, I
did not achieve the sort of protection you are after, which is a pity.
I have come up with a rather clumsy solution using a WorksheetChange
event procedure.
Everytime the sheet changes, the range of cells that change are loaded
into a two dimensional array called TargetArray. Then Cell A1 is copied
and pasted into the range of changed cells (Called Target by excel).
This resets the conditional formatting that may have been overwritten
if the change was the result of the user pasting instead of typing.
Then the values stored in TargetArray are fed into the range of changed
cells.
One important assumption is that A1 is conditionally formatted with the
=LEN(A1)>38 formula and that A1 is not a cell that the user can paste
into to destroy its conditional formatting. This will be satisfied if
A1 is locked and the worksheet is protected.
If you don't want A1 to be set up this way then, in the code, change
any reference to A1 to an appropriate cell address.
The code turned out to be a lot more complicated than I had hoped.
My original idea was to have the code do the following...
When the sheet changes, copy A1 then paste special paste formatting
into the changed cells. Unfortunately, even though the code says
exactly that (Target.PasteSpecial Paste:=xlPasteFormats), that is not
all that happens, for some reason A1's value is also pasted.
This meant I had to store the values pasted in by the user into an
array, reset the conditional formatting by pasting A1, then put the
pasted values back into the cells from the array.
If you want to try out this solution then..
1. Copy the code below
2. Right click the worksheet's Sheet Tab then select "View Code" from
the contextual popup.
3. paste the code in place into the Sheet's Code Module, which is the
white space under the boxes with the headings "(General)" on the left
and "(Declarations)" on the right
4. Press Alt + F11 to retun to the worksheet
To test it out, on another sheet, apply conditional formatting to some
cells using a different formula or different cell colour. Copy some or
all of those cells then paste them into the original worksheet.
Hopefully your conditional formatting for len>38 giving red will still
be in place. (It worked for me)
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error GoTo ERRORTRAP
Dim TargetArray As Variant
Dim I As Long, J As Long
Dim TargetRows As Long
Dim TargetColumns As Long
TargetRows = Target.Rows.Count
TargetColumns = Target.Columns.Count
ReDim TargetArray(TargetRows, TargetColumns)
For I = 1 To TargetRows
For J = 1 To TargetColumns
TargetArray(I, J) = Target.Cells(I, J).Value
Next
Next
Range("A1").Copy
Target.PasteSpecial Paste:=xlPasteFormats
For I = 1 To TargetRows
For J = 1 To TargetColumns
Target.Cells(I, J) = TargetArray(I, J)
Next
Next
Application.EnableEvents = True
Application.CutCopyMode = False
Exit Sub
ERRORTRAP: Application.EnableEvents = True
Application.CutCopyMode = False
End Sub
Ken Johnson