|Excel And SQL .com|
Home > VBA Macros
How to lock cells to protect data in a worksheet
When excel based data entry applications, you would like protech the worksheet, so users will not be able to change the layout and format of the sheet, but be able to edit certain cells and ranges to capture the user entered data.
This can be accomplished by setting the locked property of the cells to false and then protecting the entire worksheet.
Sub LockCells() With Worksheets("Sheet1") 'Create some test data .Range("A1") = "This Cell will be protected " .Range("A2") = "This Cell will be protected " .Range("C4") = "This Cell is NOT protected " .Range("D5") = "This Cell is NOT protected " 'First unlock the cells you like to be editable .Range("C4:D5").Locked = False 'Now protect the worksheet with UserInterfaceOnly set to true Worksheets("Sheet1").Protect UserInterfaceOnly:=True 'After running this macro, you will NOT be able 'to edit any cells outside the range C4:D5 End With End Sub
|© All Rights Reserved. ExcelAndSql.com|