returning to the previous cell color

Rhots

Board Regular
Joined
Nov 14, 2006
Messages
100
Hi, I am using the following code (I found here), to test for unlocked cells when I am creating a sheet. (I'm really new at this) I have 2 issues. First this (below) doesn't end itself, I have to escape or pick another cell.
Second, I would like another button to return cells to the previous color after I have corrected any locked/unlocked cell errors.
Thanks,
Rick

Private Sub CommandButton2_Click()
Sheets("bf calcs").Select

Sheets("bf calcs").Unprotect ""
For Each rCell In Sheets("bf calcs").Range("A11:d41").Cells

If rCell.Locked = False Then rCell.Interior.ColorIndex = 5
Next rCell
Sheets("bf calcs").Protect ""

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this
Code:
Private Sub CommandButton2_Click()
    Dim rCell As Range
 
    Sheets("bf calcs").Unprotect
    For Each rCell In Sheets("bf calcs").Range("A11:D41").Cells
        If rCell.Locked = False Then
            rCell.Interior.ColorIndex = 5
        Else:
            rCell.Interior.ColorIndex = 0
        End If
    Next rCell
 
    Sheets("bf calcs").Protect ""
End Sub

If you want it to update automatically without having to continually press the command button then put this in a standard module:
Code:
Option Explicit
Sub Check_Cell_Protection()
    Dim rCell As Range
 
    Sheets("bf calcs").Unprotect
    For Each rCell In Sheets("bf calcs").Range("A11:D41").Cells
        If rCell.Locked = False Then
            rCell.Interior.ColorIndex = 5
        Else:
            rCell.Interior.ColorIndex = 0
        End If
    Next rCell
 
    Sheets("bf calcs").Protect ""
End Sub

then add this in the sheet module that you want it to work in (or sheets if there are mulitple):

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Run "Check_Cell_Protection"
End Sub
That way anytime a change is made in that particular sheet, it will update the cell color

HTH
 
Upvote 0
Thanks for the reply, but what I am looking for is the ability to change the color back to previous. If I have cells a certain color, then the "protection check" changes a cell with that color, I want to be able to have it reset to original.
Thanks,
Rick
 
Upvote 0
how about this?
Code:
Option Explicit
Sub Check_Cell_Protection()
    Dim rCell   As Range
    Dim origColor  As Integer
 
    Sheets("bf calcs").Unprotect
    For Each rCell In Sheets("bf calcs").Range("A11:D41").Cells
    
        oColor = rCell.Interior.ColorIndex
        
        If rCell.Locked = False Then
            rCell.Interior.ColorIndex = 5
        Else:
            rCell.Interior.ColorIndex = origColor
        End If
    Next rCell
 
    Sheets("bf calcs").Protect ""
End Sub
 
Upvote 0
Not what I need yet, it does change the color for unlocked cells okay, but when I lock them ( as I would if changing incorrect cells) and run it again, it just changes it to the default "white" cell, not the original color I would have had in that cell.
Thanks for your help by the way,
Rick
 
Upvote 0
Sorry, I had a typo in that code... try this:
Code:
Option Explicit
Sub Check_Cell_Protection()
    Dim rCell   As Range
    Dim origColor  As Integer
 
    Sheets("bf calcs").Unprotect
    For Each rCell In Sheets("bf calcs").Range("A11:D41").Cells
 
        origColor = rCell.Interior.ColorIndex
 
        If rCell.Locked = False Then
            rCell.Interior.ColorIndex = 5
        Else:
            rCell.Interior.ColorIndex = origColor
        End If
    Next rCell
 
    Sheets("bf calcs").Protect ""
End Sub

Nvmd. Now this one is just "recording" the locked cell color and so is not changing. Someone else may have to chime in to get over that hurdle.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top