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?
- Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
- More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
- Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
- Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
- Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
- Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
- More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
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 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:
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
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 6 months agoBonjour, 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
- To post as a guest, your comment is unpublished.· 1 years agoHow 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.· 1 years agoCan 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.· 1 years agoBonjour,
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.