Skip to main content

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

Author: Xiaoyang Last Modified: 2020-07-24

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
    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
        Else
            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

Best Office Productivity Tools

ūü§Ė Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions‚Ķ
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, 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!
Comments (4)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bonjour, y a t il une astuce quand la feuille est protégée d utiliser le panel des couleurs dans l onglet accueil pour mettre un fond de couleur sur les cases non protégés merci
This comment was minimized by the moderator on the site
How do you run this in a loop, so that all cells in all sheets that are not the specific color will be protected?
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
Bonjour,
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
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations