vba and protected sheet

A1058

New Member
Joined
Sep 13, 2006
Messages
46
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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

bsnapool

Active Member
Joined
Jul 10, 2006
Messages
452
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"
 

A1058

New Member
Joined
Sep 13, 2006
Messages
46
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
---------------------------------
 

bsnapool

Active Member
Joined
Jul 10, 2006
Messages
452
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
 

A1058

New Member
Joined
Sep 13, 2006
Messages
46

ADVERTISEMENT

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

A1058

New Member
Joined
Sep 13, 2006
Messages
46
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
------------------------
 

Forum statistics

Threads
1,136,266
Messages
5,674,727
Members
419,523
Latest member
Urnovio

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
Top