I have been trying different approaches to protect a worksheet from being changed after data entry. The behavior I am trying to achieve is to allow users to make entries into a cell but then lock that cell from being edited after data entry. The simplest code I’ve come accross is the following
Private Sub Worksheet_Change (ByVal Target as Range)
ActiveSheet.Unprotect
Target.Locked = True
ActiveSheet.Protect
End Sub
This does almost what I want except that I have to set up the sheet with unlocked cells to allow for data entry. I’ve also discovered the “ActiveSheet.Unprotect Password:= “some-string” if I want to use a password.
Two questions:
- How does one use UserInterfaceOnly to have a completely locked spreadsheet at the outset, unlock a specified range of cells, which are then locked following data entry.
- Is it it possible to add to the sub so that there would be additional functionality (ie. I would eventually also like to prompt user for a reason when they manually unlock the spreadsheet with a password) or would this have to be a separate Sub.
My user level is novice if you couldn’t tell by my questions.
I initially tried locking all cells and then entered the Private sub
Private Sub Worksheet_Change (ByVal Target as Range)
With ActiveSheet.Unprotect.Cells.Locked = False
For Each Cell In ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect
End With
End Sub
But this requires all cells to be unlocked and the spreadsheet to be unprotected at the outset. I need the sheet to locked and protected at the outset