Skip to main content

Excel VBA lock cell values

Excel VBA to lock cell values but allowing user to copy the cell value.

Locking a cell or protecting a sheet will lock the cells and make it as read only and user also has  to remember the password.

So if the user forgot the password, whatever values on the cell or cells needs to type or do again  the worksheet.

VBA code below will lock the cell value allowing user to copy the value or even modify it but the moment the cell is not active anymore, then VBA will put back whatever values that is on the code.

Even if the macro has a password, the user don't need to type or the worksheet. Since the user will be able to copy the value.

To protect a range, just adjust  the range value.

=====================

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Me.Range("B1")) Is Nothing Then Exit Sub

    Application.EnableEvents = False


 If Range("B1") <> "LA Lakers Simply the best" Then

    Cells(1, 2).Value = "LA Lakers Simply the best"

    MsgBox "Please don't change the cell value again. I will put back my Favorite NBA Team ""LA Lakers"", Through the years!!!"

     End If

    Application.EnableEvents = True              

                    
End Sub



Comments