VBA to lock adjacent cells if not blank

lander2

New Member
Joined
May 4, 2017
Messages
24
Office Version
  1. 2010
Platform
  1. Windows
Hello

An easy one I'm sure but I have not found a solution to this anywhere.

I've made a holiday tracker for staff to add their own leave, I've protected cells and added buttons including one the manager can click to enter the password and unlock for authorisation. Once holiday on a particular row has been authorised by the manager entering their name against that row, say in column G, I need the cell property for B to G on that row to be switched to locked. The protection gets turned back on when saved already so it's just locking those cells I can't do.

I've found the following code but not sure how to modify to include the full range e.g. B18:G50.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Target.Value <> "" Then
    ActiveSheet.Protect Contents:=False
Range(Target.Address).Locked = True
    ActiveSheet.Protect Contents:=True
End If
End Sub

Hope that makes sense and thanks for looking.
 
I
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 7 Then
      If Target.Value <> "" Then
         Intersect(Target.EntireRow, Range("B:G")).Locked = True
      Else
         Intersect(Target.EntireRow, Range("B:C,E:F")).Locked = False
      End If
   End If
End Sub
That is awesome. Thank you for your help with this. I did manage to get it working by tinkering with the previous code but this is so much better so used this instead. Thanks again.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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