Locking cells in a row based on another cell's content

WGardner

New Member
Joined
Jun 26, 2020
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
I am trying to figure out a way in VBA to lock cells in columns A:C if the corresponding cell for that row in column D reads "Complete". So If D2 reads "Complete", A2:C2 will be locked, if D3 reads "Complete" then A3:C3 will be locked, and so on. Similarly, if D2 reads "Open" or is blank, cells A2:C2 would not be locked/ would be unlocked.

In my workbook, cells in column A can only be a date, cells in column B can only be a time, cells in column C can be any text value, and cells in column D can be "Open", "Complete", or blank.

I have seen some VBA codes to lock the entire sheet based on the value of a cell, and a code that locked a few cells in a column based on a cell, but have not been able to find anything that would work row by row like this. I have also seen a solution using data validation, but since cells A and B already have a data validation restricting to date and time I cannot get this alternative method to work.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
@WGardner Welcome.
Can you enlighten us on......
Typically, how large is your dataset in terms of rows?
How often will the dataset be updated and need the for the locking to be revised?
How will it be updated, single row entry/editing or multi-row copy paste?
 
Upvote 0
So the actual spreadsheet goes from A to O, with O being the one that is either Open/ Complete/ blank. Rows continue to increase as we are tracking emails and calls to a hotline. Entries are made throughout the week, and I learned today that some people are entering data at night. What we are trying to do is make it so once an inquiry has been followed-up on and marked complete someone can't go back in and change any of the data in that row. Data is entered in single cells by row.
 
Upvote 0
All cells are Locked by default.
Firstly, you will need to run something like below to unlock columns A:O and then selectively lock on the basis of column O value

VBA Code:
Sub Initial_Locking()
' Unlock Columns A:O
ActiveSheet.Unprotect
Columns("A:O").Locked = False
  
'Assume last row of data is determinable from column A

LR = Range("A" & Rows.Count).End(xlUp).Row
For r = 2 To LR
If UCase(Range("O" & r)) = "COMPLETE" Then Range("A" & r & ":O" & r).Locked = True
Next

ActiveSheet.Protect
End Sub

Then you could have Worksheet Change Event code to automatically check for updates in column O and to lock the row as required.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Check if only single cell changed and if is column O
If Target.Cells.Count > 1 Then Exit Sub
If Not Target.Column = 15 Then Exit Sub

'Unprotect sheet
ActiveSheet.Unprotect
'Set locking according to changed value
r = Target.Row
Range("A" & r & ":O" & r).Locked = (UCase(Target) = "COMPLETE")
'Reprotect
ActiveSheet.Protect
End Sub

Both assume that there is no Password being used.
Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
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