Lock Specific Cells if Text entered in another

NeilBarn1701

New Member
Joined
Aug 8, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I hope someone can help me with this issue.

I need to create a timesheet for staff to complete (see attached)
In cell AI19 where at present the word 'Manager' sits the line manager will change this word to his initials.
After this is entered I would like the cells that correspond to that week e.g. A4:AC17 to lock so they can't be adjusted by the user.

This is then repeated for the next week and so on.

I have cleared the worksheet of all protected cells and added the following VBA:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("AI19") = "Manager" Then
Range("A4:AC17").Locked = False
ElseIf Range("AI19") = "NB" Then
Range("A4:AC17").Locked = True
End If
End Sub

However, when I protect the sheet and try and enter data with or without the cell AI19 having the word 'Manager' I get a VBA error Run Time error '1004' Unable to set the locked property of the Range class.

I have checked, their are merged cells but not in the range given any help would be much appreciated.

Thanks
Neil
 

Attachments

  • Screenshot 2022-08-08 111650.png
    Screenshot 2022-08-08 111650.png
    29.8 KB · Views: 22

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi, you need to unprotect the sheet before setting the locked property and then protect it again afterwards - you might also consider only having the code run when the cell you are testing is changed.

Something like..

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AI19")) Is Nothing Then
    Me.Unprotect
    If Range("AI19") = "Manager" Then
        Range("A4:AC17").Locked = False
    ElseIf Range("AI19") = "NB" Then
        Range("A4:AC17").Locked = True
    End If
    Me.Protect
End If
End Sub
 
Upvote 0
Solution
Hi, you need to unprotect the sheet before setting the locked property and then protect it again afterwards - you might also consider only having the code run when the cell you are testing is changed.

Something like..

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AI19")) Is Nothing Then
    Me.Unprotect
    If Range("AI19") = "Manager" Then
        Range("A4:AC17").Locked = False
    ElseIf Range("AI19") = "NB" Then
        Range("A4:AC17").Locked = True
    End If
    Me.Protect
End If
End Sub
Thank you for your assistance, much appreciated. Since the manager will need to lock each week after it is completed; would I duplicate each If statement with the new range each time. I need to prepare this for 2023 so will need to have the VBA ready for each of the 52 weeks next year. 😟
 
Upvote 0
Hi, I don't really understand the follow up question to be honest - but if you want the range that is being locked to be dynamic based on the current week or year (or something else) then if you are able to explain the logic I'm sure someone could help write the VBA.

Note, it would probably be best if you created a new thread for the new question.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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