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.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 7 And Target.Value <> "" Then
      Me.Unprotect "Pword"
      Intersect(Target.EntireRow, Range("B:G")).Locked = True
      Me.Protect "Pword"
   End If
End Sub
Change Pword to match your password
 
Upvote 0
Thanks, I don't need password to be added here, l already got the workbook locking and unlocking working. Just need the property? (which is usually accessed via format cells) to switch to locked so those cells are protected on locking.

Thanks
 
Upvote 0
If the sheet is password protected you need to unlock it in order to change the locked property of the cells.
 
Upvote 0
The manager clicks a button which asks for the password which, if correct, unlocks the workbook. That's working. I'm looking for code to the change cell properties of the row so that they lock once the manager has entered something into that row in column G. The workbook will be protected on save - that code is already working.

Thanks
 
Upvote 0
In that case just remove the Unprotect & Protect lines.
 
Upvote 0
If you use in the protect parameters UserInterfaceOnly:=True you wont have to unlock most of the times. Certain Table/Listobjects actions though still needs it though and at reloading the workbook the protection with UserInterfaceOnly needs reassertion, so I made it part of the Workbook_Open procedure in the ThisWorkbook class module.
 
Upvote 0
In that case just remove the Unprotect & Protect lines.

Oh wow,
Tried this and it works great.
Can you add a line to unlock cells b :c and e:f if the authorisation in column g is removed.

I've tried to adapt your code that do it but it just breaks.

Thanks
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,215,359
Messages
6,124,488
Members
449,166
Latest member
hokjock

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