LOCK CELL AFTER DATA IS ENTERED

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
Is there a way to automatically lock a cell after a value is entered?

Thanks for the help,
Mike
 
Hi
Is there a way to automatically lock a cell after a value is entered?

Thanks for the help,
Mike

You can Copy the cell and then Paste Special / Values, and then
Format the cell to be Locked and then Protect the Worksheet, but
that's hardly automatic.

ed
 
To see how this would work, open a new workbook. Highlight cells A1:A20 and
use Format-Cells-Protection tab and UNCHECK the locked box.

Right click the Sheet Tab and select View Code and then paste in this
subroutine.

Use Tools-Protection-Protect Sheet to assign the password 'thepassword'

You can only add data to to cells A1:A20 and once you add data to any of
those cells, the cell becomes locked preventing any further modification.

Modify the range and password in the subroutine to suit your application.


Private Sub Worksheet_Change(ByVal Target As Excel.Range)

' Place this code in the worksheet code module.
' The subroutine unprotects the sheet and IF an entry is made
' in an empty ("") cell, the cell is locked and then the
' sheet's protection is turned back on. Any further
' attempts to edit the cell generate the password msgbox.
' You can set the range to one cell ("A1") or an area ("A1:Z300").
' 1. Use Format - Cells - Protection to unlock the cells
' in the range where one time entries are to be allowed.
' 2. Protect the worksheet with the same password as
' you use twice in the following subroutine (thepassword).


On Error GoTo justenditall

Application.EnableEvents = False
If Not Intersect(Target, Range("A1:A20")) Is Nothing Then
If Target.Value <> "" Then
ActiveSheet.Unprotect Password:="thepassword"
Target.Locked = True
End If
End If

ActiveSheet.Protect Password:="thepassword"

justenditall:
Application.EnableEvents = True
End Sub
 
If there a way to only lock the cells after the user had saved the file? I tried your macro and it work really good, too good. :)

The thing is, if a user inputs the wrong data he/she will not be able to go back and make the necessary changes. Is there a way to allow the user to verify thier data entry before locking the cell?

Thanks in advance

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Post in the original thread.

Do not start a new one with any details whatsoever.

Whose macro? What does it do?

You could lock cells as the user saved the file using a BeforeSave event.

Yes, you can verify if the data is correct before locking the cell but what
would constitute "correct data"?


Gord Dibben MS Excel MVP
 
Hi
This macro does the work. it protects the cells when saved. But the problem is it protects all cells that has a value.
Can anyone help me to amend this code. i want column B not be locked even if it has any values..

Here is the code


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

If ActiveSheet.Name = "Sheet5" Then
On Error Resume Next
'Resume to next line if any error occurs
Dim Cell As Range
With ActiveSheet
'first of all unprotect the entire
'sheet and unlock all cells
.Unprotect Password:=""
.Cells.Locked = False
'Now search for non blank cells
'and lock them and unlock blank cells
For Each Cell In ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:=""
End With
Exit Sub
End If
End Sub

Please help me this is the last problem in my project before completion..
 
Back
Top