Avatar of davie jonea
davie joneaFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Quick way to protect/unprotect cells

HI

Is there any quick way I can protect and unprotect cells without selecting them all.

What im trying to do is all the cells that are YELLOW on my sheet I need them UNPROTECTED and the rest of the worksheet protected.


Any Ideas?
Microsoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Martin Liss
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

This assumes that the yellow cells are the vbYellow color.
Sub LockYellow()
Dim cel As Range

' Choose the cells that you DON'T want protected
For Each cel In ActiveSheet.UsedRange.Cells
    If cel.Interior.Color <> vbYellow Then
        cel.Locked = False
    End If
Next

' Protect all the rest
Worksheets("Sheet1").Protect Password:="Secret", UserInterfaceOnly:=True
End Sub

Open in new window

Sub UnlockAll()
Worksheets("Sheet1").Unprotect Password:="Secret" ', UserInterfaceOnly:=True
End Sub

Open in new window

29222256.xlsm
Avatar of davie jonea
davie jonea
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Sorry Think I confused you. What im looking for is to be able to select the whole sheet and the cells that are yellow colour I need UNPROTECTED. or am I missing a bit I need to add to the macro
Avatar of Peter Chan
Peter Chan
Flag of Hong Kong image

If you want to protect the cell, you can set cell's Locked property to be True. And Locked property to be False, if you want to unprotect the cell.

After that you can then protect the sheet using the code below.

Sub Protect_Workbook(Para_Message As String)
    Application.ScreenUpdating = True
    Worksheets("Main Sheet").Protect Password:="12345"
End Sub

Open in new window


Here is the way to unprotect the sheet.


Sub Unprotect_Workbook(Para_Message As String)
    Application.ScreenUpdating = False
    Worksheets("Main Sheet").Unprotect Password:="12345"
End Sub

Open in new window


Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Chnge line 6 from

If cel.Interior.Color <> vbYellow Then

Open in new window


to

If cel.Interior.Color = vbYellow Then

Open in new window

Avatar of davie jonea
davie jonea
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Sorry changed that and now comes up with run time error 424.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Did you include the Dim cel As Range line in my code?

Please supply a small sample workbook.
Avatar of davie jonea
davie jonea
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

This is the actual workbook and im looking at locking everything apart from yellow cells on Data Input sheet. Copy of Copy of CubicSheet V1.3.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Distinguished Expert in Excel 2018
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2020
              Experts Exchange Top Expert VBA 2018 to 2020
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo