VBA to lock cells in different ranges

NeilBarn1701

New Member
Joined
Aug 8, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,

I am building timesheets for the company staff to complete their hours and jobs, see below.

1660202563100.png


The sheet has Monday to the Weekend across in columns with room for estimate hours, actual hours, the task and job number.
The next week with the same columns is underneath etc.

To the side each weekly block has a totalizer, request for overtime and the part I am struggling with management approval and sign off for the work done this week (it is in merged cell AI20 for Week 31)

What I have been tasked to do is each of these weeks when the line manager changes the word 'Manager' to his initials, it locks that weeks work so the hours cannot be altered unless with the protected password. The same thing is then required for the end of the next week, and then the week after that etc.

I have this code so far that @FormR member prepared for me:

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


Thee issue that I have is the code only looks at that one single week. If I try and replicate the code to include the next week in a separate If statement e.g. Range A25:AC38 then I get errors, any help would be appreciated.

Thanks

Neil
 
Last edited by a moderator:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This could be a solution for you as long as the structure of your weekly blocks is always the same: cells with validation (Manager/NB) in the column AI will always be 21 rows apart starting from row 19 (as per your attached example).
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AI:AI")) Is Nothing And (Target.Row + 2) Mod 21 = 0 Then
        Me.Unprotect
        If Target = "Manager" Then
            Range(Cells(Target.Row - 15, "A"), Cells(Target.Row - 2, "AC")).Locked = False
        ElseIf Target = "NB" Then
            Range(Cells(Target.Row - 15, "A"), Cells(Target.Row - 2, "AC")).Locked = True
        End If
        Me.Protect
    End If
End Sub
PS. tested on Windows.
 
Last edited:
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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