# vba and protected sheet

#### A1058

##### New Member
hello guys hoping someone can solve this for me..
im using this in vba to color some cells based on value of another cell.
it works until i protect the sheet.. is there a way to make this work even when sheet is protected? all cells mentioned are unlocked even when sheet is protected.
-----------------------
If [a42].Value <= 0 Then
[h12:h14].Interior.ColorIndex = 0
End If
If [a42].Value > 0 And [a42].Value <= 1 Then
[h12].Interior.ColorIndex = 6
End If
If [a42].Value > 1 And [a42].Value <= 2 Then
[h12:h13].Interior.ColorIndex = 6
End If
If [a42].Value > 2 And [a42].Value <= 3 Then
[h12:h14].Interior.ColorIndex = 6
End If
------------------------------------

### Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Could you not just unprotect, run the code and get it to protect it again after the code has ran...

Code:
`````` With Sh
.Unprotect "password"
YOUR CODE..................
.Protect "password"``````

tried that just now. doesnt seem to work for me unless im doing it wrong
VBA is highlighting the .Unprotect line when the above mentioned statements become true..vba is saying object required.

---------------------------
With sh
.Unprotect "password"
If [a42].Value <= 0 Then
[h12:h14].Interior.ColorIndex = 0
End If
If [a42].Value > 0 And [a42].Value <= 1 Then
[h12].Interior.ColorIndex = 6
End If
If [a42].Value > 1 And [a42].Value <= 2 Then
[h12:h13].Interior.ColorIndex = 6
End If
If [a42].Value > 2 And [a42].Value <= 3 Then
[h12:h14].Interior.ColorIndex = 6
End If
.Protect "password"
end with
---------------------------------

Try:

Code:
``````Private Sub
Dim Sh As Worksheet
With Sh
.Unprotect "password"
If [a42].Value <= 0 Then
[h12:h14].Interior.ColorIndex = 0
End If
If [a42].Value > 0 And [a42].Value <= 1 Then
[h12].Interior.ColorIndex = 6
End If
If [a42].Value > 1 And [a42].Value <= 2 Then
[h12:h13].Interior.ColorIndex = 6
End If
If [a42].Value > 2 And [a42].Value <= 3 Then
[h12:h14].Interior.ColorIndex = 6
End If
.Protect "password"
End With

End Sub``````

thank you for your replies
its saying object variable or with block variable not set on the
.Unprotect line

i think i got it
thank you very much
this did it
--------------------------
Dim Sh As Worksheet
With Sh
ActiveSheet.Unprotect "password"
If [a42].Value <= 0 Then
[h12:h14].Interior.ColorIndex = 0
End If
If [a42].Value > 0 And [a42].Value <= 1 Then
[h12].Interior.ColorIndex = 6
End If
If [a42].Value > 1 And [a42].Value <= 2 Then
[h12:h13].Interior.ColorIndex = 6
End If
If [a42].Value > 2 And [a42].Value <= 3 Then
[h12:h14].Interior.ColorIndex = 6
End If
ActiveSheet.Protect "password"
End With
------------------------

I'm only learning myself, No problem..

Replies
2
Views
75
Replies
1
Views
219
Replies
2
Views
266
Replies
21
Views
454
Replies
1
Views
120

Threads
1,221,185
Messages
6,158,408
Members
451,490
Latest member
desktopace

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

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