I've tried all 3 examples of code in this thread, but the only one that doesn't error is from post #3.
After trying post #12 code, I get this error when a locked cell is selected and after the custom message is displayed:
"Runtime error 1004: The cell or chart you are trying to change is on a protected sheet. To make a change unprotected sheet. You might be requested to enter a password."
NOTE: When using the code from post #12, doesn't matter if I select End or Debug or clicking the VBA Reset button, I have to close and reopened Excel in order to get any code to work.
After trying post #4 code, I get this error when any locked/unlocked cell is selected:
"Runtime error 1004: Method 'Range' of object'_Worksheet' failed."
Therefore, the only code that works is from post #3, but it moves the focus to A10 after clicking OK, rather then keeping it on the originally selected cell. It also doesn't allow for using the PgUp, PgDown and Arrow keys to navigate as mentioned in post #8.
I was able to modify the code from post #3 to keep the focus on the cell that triggered the error as show here, but it triggers the error when the cell is selected, rather than only if an attempt to modify it is done.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim goodRng As Range
Application.EnableEvents = False
If Target.Locked Then
Application.EnableEvents = True
MsgBox "Stop!" & vbNewLine & vbNewLine & _
"The cell(s) you are trying to alter are protected" & vbNewLine & _
"and should not be altered without prior" & vbNewLine & _
"authorization." & vbNewLine & vbNewLine & _
"Thank you," & vbNewLine & _
"Management", vbCritical, "STOP!"
End If
Application.EnableEvents = True
End Sub
My objectives:
1) Range is entire sheet
2) As mentioned in post #8, I want the users to be able to use the PgUp, PgDown and Arrow keys to navigate. Therefore, only trigger custom error if they attempt to modify a locked cell.
3) Leave the user on the cell that generated the error