Thanks, teylyn. That is indeed a great (and simple) solution for a lot of cell locking purposes! And it has the advantage that it is more conventional.
To clarify his answer a little bit, there is more than one way to password "protect" a document. If you go to File --> Info, you want to "Protect Current Sheet" or "Protect Workbook Structure," rather than "Encrypt with Password" (which will require a password from anyone who opens the document). You can also go to the Review tab and click Protect Sheet or Protect Workbook.
There are, however, a few downsides to this solution:
- It locks a cell so that even VBA cannot change it. So you have to unlock a cell if you want to be able to change its value within VBA, and then relock it. But you cannot unlock a cell without first unprotecting the sheet. So it's a little convoluted if you want to use VBA very much.
A search online yields a rather hackish solution to that.
- You have to unlock what you want unlocked, because all cells are locked by default. So if you unlock only the current cells, and then you add some new data, those cells will also be locked by default. You could perhaps make a default template in which all the cells are unlocked by default. Or you could simply make sure to unlock all the cells at the beginning, before then locking what you want to lock.
- As I said, you can't unlock a cell while the worksheet is protected, so to unlock one cell does require you to unprotect the sheet, unlock the cell, and then reprotect the sheet.
- You can't lock cells conditionally.
On that note, the solution I posted was a little bit hacky, and this simpler one is better for man purposes, however the earlier one does allow VBA to change the contents of the cells, and it also allows some extra flexibility for locking cells based on certain conditions. For instance, you could allow the user to change the cell values only and not their formatting. (If your condition is met, then you undo the changes to the cell, and if it's not met, then you don't undo them.)
If you want to permit only say values 1 through 5 in a cell, then Data Validation might be a better way. Although again, that won't prevent the user from deleting the cell contents altogether.
If you do use a password, then of course make sure that you don't forget it, because that would be an added headache. Since it's possible to protect a sheet without using a password, I think that's probably a good solution in many cases, or at least until a final version is saved.
Dan
Bookmarks