Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


How to protect or lock cell values based on background color?

Supposing, I have a large worksheet and multiple cells are filled with different background colors, now, I want to lock or protect the cells based on certain filled color, such as locking or protecting all cells with red color. Are there any good methods to deal with this task in Excel?

Protect or lock cell values based on background color with VBA code

arrow blue right bubble Protect or lock cell values based on background color with VBA code

For example, I want to lock and protect all red cells, in order to prevent other users from modifying these cell values, the following VBA code can solve this job for you, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Protect or lock cell values based on color:

Sub lockcellsbycolor()
'Updateby Extendoffice 20161027
    Dim colorIndex As Integer
    colorIndex = 3
    Dim xRg As Range
    Application.ScreenUpdating = False
    For Each xRg In ActiveSheet.UsedRange.Cells
        Dim color As Long
        color = xRg.Interior.colorIndex
        If (color = colorIndex) Then
            xRg.Locked = True
            xRg.Locked = False
        End If
    Next xRg
    Application.ScreenUpdating = True
    MsgBox "All specified color cells have been locked!", vbInformation, "Kutools for Excel"
End Sub

Note: In the above code, the number 3 within the colorIndex = 3 script indicates the red color cells I want to lock, you can change it to other color index which you want to lock.

3. Then press F5 key to run this code, and only red color cells have been locked, other cells are unlocked in the used range of active sheet, see screenshot:

doc lock cells by color 1

4. If you want to protect them from being modified by other users, you just need to apply the Protect Sheet feature to protect this worksheet.

Tips: To get the color index number, you can apply the following User Defined Function:

Function GetColor(x As Range) As Integer
GetColor = x.Interior.ColorIndex
End Function

Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Christian · 20 days ago
    How do you run this in a loop, so that all cells in all sheets that are not the specific color will be protected?
  • To post as a guest, your comment is unpublished.
    Ben · 7 months ago
    Can this be applied to a sheet with merged cells and can it recognize a cell color that is based on conditional formatting? I want to lock cells that are color code 15 and those cells are color code 15 based on a conditional format. Thank you in advance.
  • To post as a guest, your comment is unpublished.
    Sébastien · 10 months ago
    j'aurais voulu savoir si il était possible de faire la même chose mais avec la valeur de la cellule plutôt que la couleur?
    Par exemple: Verrouiller toutes les cellules dont la valeur est "-" de la plage B2:E25.

    Merci d'avance