This tutorial demonstrates how to automatically protect data immediately after it has been entered in a cell. The steps are:
Unlock all cells in the worksheet.
- To do this select all cells (click in an empty cell and use the shortcut CTRL A to achieve this)
- Open the Format Cells dialog (CTRL 1 will achieve this). On the Protection tab untick the Locked property. Click OK.
Create the VBA code to lock cells.
- Open the Visual Basic Editor (ALT F11 will achieve this)
- In the Project Explorer (if you can’t see the Project Explorer use CTRL R to open it), double-click the sheet that you want to apply this code to.
- In the code window, you have two drop-downs. In the first select Worksheet and in the second select Change
- Delete the Worksheet_SelectionChange subprocedure as it is no longer needed
- In the Worksheet_Change subprocedure add the following code:
ActiveSheet.Unprotect Password:="YourPassword" Target.Locked = True ActiveSheet.Protect Password:="YourPassword"
- Use the shortcut ALT F11 to return to your workbook and try it out.