Point for you barryleajo - I didn't think that there may be some protected cells already filled with color (but I am still a beginner in VBA and Excel).
Anyway here is my version of code that preserves colors in protected cells:
Private prevcell As Range
to be added above all procedures in Sheet module (global variable)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Protect UserInterfaceOnly:=True
If prevcell Is Nothing Then Set prevcell = Target
If Not prevcell Is Nothing And prevcell.Locked = False Then prevcell.Interior.ColorIndex = xlNone
If Target.Locked = False Then Target.Interior.Color = RGB(221, 221, 221)
Set prevcell = Target
End Sub
I also added that line:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ActiveCell.Locked = False Then ActiveCell.Interior.ColorIndex = xlNone
End Sub
to avoid saving Workbook with unprotected cell filled with color
I don't know if the code is written properly, but it works for me
Bookmarks