Lock cells in a row if a date is entered into an adjacent cell

Simplyflorida

New Member
Joined
Feb 22, 2013
Messages
5
I have a row of data in cells A5 to P5. When a date (ie Feb 26 2013) is entered into cell Q5, I want the whole row to be locked.

Need to do this for a complete range of cells (rows 5 to 500 for example).

I need to use VBA, but am not sure of the wording.

Any ideas please.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Column = 17 Then
        ThisRow = Target.Row
            ActiveSheet.Unprotect
        If Target.Value > 0 Then
            Range("A" & ThisRow & ":P" & ThisRow).Locked = True
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        Else
            Range("A" & ThisRow & ":P" & ThisRow).Locked = False
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    End If
End Sub

Make sure you unlock all cells in the worksheet first, as cells are locked by default.
 
Upvote 0
Here's an idea until someone comes up with a better one...
First if you plan to have these fields not be able to be edited, unlock all the cells in the workbook first, and remove the ' in front of the lines in the below code, otherwise the Code will lock all those cells.
If you just want the cells locked remove the lines I've commented.
I'm also assuming the only value you expect in the field is a Date.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Worksheets("Sheet1").Unprotect
If Range("Q5").Value <> "" Then
Range("A5:P5").Locked = True
'Worksheets("Sheet1").Protect
Else
Range("A5:P5").Locked = False
'Worksheets("Sheet1").Unprotect
End If
End Sub
I know it's not the most efficient piece of code, but it does what you want.
 
Upvote 0
Works great for row 5.

Sorry to be a pain but how do I program it to work for (say) lines 5 through to 5000 as I have lots of data. Need to lock each line individually when a date is entered into Column Q.

Thanks again for you time and help
 
Upvote 0
I am working solely on row 5 at the moment but the intention is to use many rows.

I entered the following code but cells do not lock. I have data in columns A to Q and the date need to be entered in column R (column 18). When I enter a date in row R, the cells in row 5 still remain unlocked.

Cels are all unlocked to begin with.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 18 Then
ThisRow = Target.Row
ActiveSheet.Unprotect
If Target.Value > 0 Then
Range("A5:Q5").Locked = True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Else
Range("A5:Q5").Locked = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End If
End Sub


Any ideas please.
 
Upvote 0
Macro is working ok on row 5. This stops any new data being entered.

However, the macro is also preventing me from any formatting etc on rows 6 onward.

Is there a way just to protect row 5 and leave all other rows open to change.

As data and then dates are entered into rows 6,7,8 etc I want these protected from change but rows 9 onward open so that I can add data and new rows.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
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