VBA Protect All Cells that Do NOT have Interior Colour = 6

ElectricSkywalker

Board Regular
Joined
May 27, 2002
Messages
112
Hi there,

I have a spreadsheet with a number of inputs in it.

All of the input cells have an Interior Colour of 6 (yellow).

Everything else either has a formula, is a fixed digit, or is empty.

What I want....Is to create a macro so that everything with interior colour is NOT locked/protected....and everything else is.

The following was suggested to me...but is still a little slow:

Private Sub CommandButton1_Click()

Dim x As Integer
Dim y As Integer

For x = 1 To 100
For y = 1 To 100
If Cells(x, y).Interior.ColorIndex = 6 Then
Cells(x, y).Locked = False
Else: Cells(x, y).Locked = True
End If

Next y
Next x

End Sub

Thanks for your advice
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Re: VBA Protect All Cells that Do NOT have Interior Colour =

Invoking a UsedRange reference because you didn't say what range you are working with, see if this does what you want:

Application.ScreenUpdating = False
Dim cell
For Each cell In ActiveSheet.UsedRange
Select Case True
Case cell.Interior.ColorIndex = 6
cell.Locked = False
Case Else
cell.Locked = True
End Select
Next
Application.ScreenUpdating = True
 
Upvote 0
Re: VBA Protect All Cells that Do NOT have Interior Colour =

Your a lengend Tom...once again you have come through with what I was looking for.
Thanks for the tip on Selecting a range....at first I pasted your formula in...and it took a long time to run, after that, I defined my range...and it worked like a charm!!

I also noticed you used the line:

For Each cell In ActiveSheet.UsedRange

This may be a silly questions, but can I substiute ActiveSheet to something else.... as in "Workbook"???

Cheers
 
Upvote 0
Re: VBA Protect All Cells that Do NOT have Interior Colour =

Well first, thanks for the compliment.

Why are you asking what you are asking? Is it because you want to perform this action on all the sheets in your workbook? Or just some of them?
 
Upvote 0
I' m having trouble getting the code which Tom Urtis posted to work.
It is sticking at the line:

cell.Locked = True

I've tried it out on a clean new WS and it works OK but not on the WS I want to use it on.

I've run a macro to determine the usedrange and it only returns the rows, not the columns. Could that have something to do with it?

All help welcome
 
Upvote 0

Forum statistics

Threads
1,225,069
Messages
6,182,663
Members
453,131
Latest member
BeLocke

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