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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi, Cossie,

is this useful ?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Answ As String
Application.ScreenUpdating = False
Answ = MsgBox("This range 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
Application.ScreenUpdating = True
End Sub

regards,
Erik
 
Upvote 0
Try this

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rng             As Range
Dim Cell            As Object

    Set Rng = Range("A" & Target.Row & ":F" & Target.Row)
    For Each Cell In Rng
        If Cell.Value = vbNullString Then Exit Sub
    Next
    Sheets("Sheet1").Unprotect Password:="1"
    Rng.Locked = True
    Sheets("Sheet1").Protect Password:="1"

End Sub

You can add in your message here and clear either the target or the whole range if the user does not select OK.
 
Upvote 0
Hi Erik,

I used your code to try and lock Rows A through R for the targeted row. I am getting a 424 error with the "Target.Locked = True" code. Could I get some help on a code that would solve the error?
Code:
Sub Worksheet_Change()
Dim Answ As String
Application.ScreenUpdating = False
Answ = MsgBox("This range 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 "bob"
Target.LockedRow = True
ActiveSheet.Protect Password:="bob", DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0
Your declaration line is wrong - it must be as in the original posts:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Upvote 0
Your declaration line is wrong - it must be as in the original posts:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

I fixed the code, but it isn't showing up in my macro list when trying to run and test it. What would active the macro to lock the row?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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