how to locked and unlocked cells with protect sheets

Sian1

Board Regular
Joined
Nov 9, 2009
Messages
90
Hi,

Hoping some could help me. i was able to write the simple code with locked and unlocked cell based on a cell value however when i do a protect sheet function, it gave me an error "Run time Error "1004", unable to set the locked property on the range class"

basically here is the code
Private Sub worksheet_change(ByVal target As Range)
If Range("H10") = "Tier 3" Then
Range("H25:H29").Locked = True
Range("K10:K26").Locked = True
ElseIf Range("H10") = "Tier 1" Then
Range("H25:H29").Locked = False
Range("K10:K26").Locked = False
ElseIf Range("H10") = "Tier 2" Then
Range("H25:H29").Locked = False
Range("K10:K26").Locked = False
ElseIf Range("H10") = "Tier 4" Then
Range("H25:H29").Locked = True
Range("K10:K26").Locked = True
End If
End Sub


and i also have another question, in cell H13 there is a data validation function being used, drop down available for RT- with RI insurance and RT - with No RI Insurance however when the user select Tier 3 or Tier 4 in cell H10, it should only be the value - RT with NO RI insurance, how can i default that cell with the value when user select the Tier 3 or Tier 4.

thanks so much in advance
 

Attachments

  • pic 1.jpg
    pic 1.jpg
    118.2 KB · Views: 11
  • Capture.JPG
    Capture.JPG
    59.7 KB · Views: 11

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this

VBA Code:
Private Sub worksheet_change(ByVal target As Range)
  If target.Address = "$H$10" Then
    If target.CountLarge > 1 Then Exit Sub
    ActiveSheet.Unprotect
    Select Case target.Value
      Case "Tier 1", "Tier 2"
        Range("H25:H29").Locked = False
        Range("K10:K26").Locked = False
      Case "Tier 3", "Tier 4"
        Range("H25:H29").Locked = True
        Range("K10:K26").Locked = True
        Range("H13").Value = "RT with NO RI insurance"
    End Select
    ActiveSheet.Protect
  End If
End Sub
 
Upvote 0
Try this

VBA Code:
Private Sub worksheet_change(ByVal target As Range)
  If target.Address = "$H$10" Then
    If target.CountLarge > 1 Then Exit Sub
    ActiveSheet.Unprotect
    Select Case target.Value
      Case "Tier 1", "Tier 2"
        Range("H25:H29").Locked = False
        Range("K10:K26").Locked = False
      Case "Tier 3", "Tier 4"
        Range("H25:H29").Locked = True
        Range("K10:K26").Locked = True
        Range("H13").Value = "RT with NO RI insurance"
    End Select
    ActiveSheet.Protect
  End If
End Sub
Your are god send :) thanks so much
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Here an example:

Rich (BB code):
Private Sub worksheet_change(ByVal target As Range)
  If target.Address = "$H$10" Then
    If target.CountLarge > 1 Then Exit Sub
    ActiveSheet.Unprotect "abc"
    Select Case target.Value
      Case "Tier 1", "Tier 2"
        Range("H25:H29").Locked = False
        Range("K10:K26").Locked = False
      Case "Tier 3", "Tier 4"
        Range("H25:H29").Locked = True
        Range("K10:K26").Locked = True
        Range("H13").Value = "RT with NO RI insurance"
    End Select
    ActiveSheet.Protect "abc"
  End If
End Sub
 
Upvote 0
thanks so much Dave and my apology for the late reply got so sick :(

so grateful for the help sure learnt a lot from you.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Hi Dave,

Quick question, how could I add when Tier 1 or Tier 2 with cell H13 = "RT - No Insurance" (please note this cell is a drop down list), cell K24:L24 will be locked. Appreciate your help. :)


Private Sub worksheet_change(ByVal target As Range)
If target.Address = "$H$10" Then
If target.CountLarge > 1 Then Exit Sub
ActiveSheet.Unprotect
Select Case target.Value
Case "Tier 1", "Tier 2"
Range("H25:H29").Locked = False
Range("K10:K26").Locked = False
Case "Tier 3", "Tier 4"
Range("H25:H29").Locked = True
Range("K10:K26").Locked = True
Range("H13").Value = "RT with NO RI insurance"
End Select
ActiveSheet.Protect
End If
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.

Hi Dave,

Quick question, how could I add when Tier 1 or Tier 2 with cell H13 = "RT - No Insurance" (please note this cell is a drop down list), cell K24:L24 will be locked. Appreciate your help. :)


Private Sub worksheet_change(ByVal target As Range)
If target.Address = "$H$10" Then
If target.CountLarge > 1 Then Exit Sub
ActiveSheet.Unprotect
Select Case target.Value
Case "Tier 1", "Tier 2"
Range("H25:H29").Locked = False
Range("K10:K26").Locked = False
Case "Tier 3", "Tier 4"
Range("H25:H29").Locked = True
Range("K10:K26").Locked = True
Range("H13").Value = "RT with NO RI insurance"
End Select
ActiveSheet.Protect
End If
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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