When I first open my workbook I call this function to protect specific cell ranges and cells from the user:
' Sets protection on sheet
Public Sub LockCells_TXN_CLIENT_ORDER_ENTRY()
ActiveSheet.Unprotect Password:="abc"
ActiveSheet.Cells.Locked = False
' Lock some cells here
ActiveSheet.Protect Password:="abc"
End Sub
However, upon calling the following Subrouting to clear data from a range of rows:
Private Sub ClearData_Click()
' Declare some variables
For currentRow = ActiveSheet.Range("STATUS_FIELDS").Row To totalRows
ActiveSheet.Cells(currentRow, ActiveSheet.Range("STATUS_FIELDS").Column).Resize(1, 14).Clear
Next currentRow
End Sub
Cells that were not specified to be protected now becomes protected and these cells are the exact same cells that the ClearData_Click() function affected.
Could someone please explain to me why this happens and how to avoid this?
Your use of
.Clearis causing the problem.Either use
.ClearContentsinstead, or re-set the locked property after you clear the range..Clearclears the entire object, including the part where you set its locked property to false.