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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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"
 
Upvote 0
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
---------------------------------
 
Upvote 0
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
 
Upvote 0
thank you for your replies
its saying object variable or with block variable not set on the
.Unprotect line
 
Upvote 0
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
------------------------
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,287
Members
448,562
Latest member
Flashbond

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