Locking cells after entry using VBA

Cossie

Active Member
Joined
May 6, 2002
Messages
328
G'day everyone ~ Thanks again for your assistance :help:

I am trying to write some code that will allow me to lock cells after data has been entered.

What i have in my example code below locks cell b1. What i would like is to lock a range of cells after a user has entered some information.

eg user enters data in cells A1:F1 and then these cells lock. The next user enters data into A2:F2 and thes cells then lock and the next users enters data into A3:F3 and these cells then lock etc etc etc.

This is what I have at the moment:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim Answ, Rng As Range

Application.ScreenUpdating = False
Set Rng = Application.Intersect(Target, Range("b1"))
If Target.Cells.Count > 1 Then Exit Sub
If Not Rng Is Nothing Then
Answ = MsgBox("This cell will now be locked.", vbOKCancel, "Confirm Change")
If Answ <> vbOK Then
Application.EnableEvents = False
Target.ClearContents 'clear contents if cancel is pressed
Application.EnableEvents = True
Exit Sub
End If
ActiveSheet.Unprotect "1"
Target.Locked = True
ActiveSheet.Protect Password:="1", DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
Application.ScreenUpdating = True
End Sub
 
It needs to be in the worksheet code module for the relevant sheet. It will run automatically when you make changes to the sheet.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
It needs to be in the worksheet code module for the relevant sheet. It will run automatically when you make changes to the sheet.

This code seems to lock all cells. What if you wanna lock only the one that you have entered data in?
Kelly
 
Upvote 0
You need to make sure that all the cells you want editable at the start have the Locked property set to False (it's True by default).
 
Upvote 0
Okay done. So what it i dont want to be able to select a locked cell?
 
Upvote 0
Set the Worksheet's EnableSelection property to xlUnlockedCells.
 
Upvote 0
Rich (BB code):
ActiveSheet.Unprotect "bob"
Target.LockedRow = True
Activesheet.EnableSelection = xlUnlockedCells
ActiveSheet.Protect Password:="bob", DrawingObjects:=True, Contents:=True, Scenarios:=True
 
Upvote 0

Forum statistics

Threads
1,224,844
Messages
6,181,291
Members
453,030
Latest member
PG626

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