By default, all cells on a worksheet are locked; however, this has no effect unless worksheet protection has been applied. Here is a very easy way to apply worksheet protection so that only formula cells are locked and protected.
- Unlock all cells.
Select Cells Containing Formulas with Go To Special - Lock cells containing formulas.
- Protect the sheet and content of locked cells.
Allow formatting on unlocked cells on a protected sheet - Using VBA to Protect Sheet If Locked Cells Found
1. Unlock Cells
- Select all cells on your worksheet by pressing
Ctrl+A
or⌘+A
. - Right-click on any cell and choose
Format Cells
from the context menu. - Go to the
Protection
tab and uncheck theLocked
option. - Click OK.
After removing the lock for all cells, your next step is to select only the cells that contain formulas that you want to lock and protect.
Select Only Cells That Contain Formulas
The Go To Special can select all of the formula cells in your worksheet with few clicks:
- Select any single cell on the sheet.
- Press
Ctrl+G
orF5
to openGo To
dialog box. - Click Special to open the
Go To Special
dialog box. - Select
Formulas
If needed, limit the formulas to the subtypes underneath. - Click OK.
After selecting formula cells, your next step is to lock and protect these cells.
2. Lock Cells Containing Formula
Now we repeat the steps we did to unlock all cells, but this time we lock cells containing formulas. With only the formula cells selected:
- Right-click on a selected formula cell and choose
Format Cells
from the context menu. - The
Format Cells
dialog box will be displayed. - Go to the
Protection
tab and CHECK theLocked
option. - Click OK.
After locking formula cells, your next step is to protect the worksheet to enabled protection for LOCKED cells.
3. Protect Worksheet
Now go to the Review
tab and click on Protect Sheet
. You’ll see a dialog box such as that in the figure:
Apply a password if required and click OK.
The preceding method certainly saves a lot of time and eliminates possible errors in locating formulas so that you can protect them.
Allow formatting on unlocked cells on a protected sheet
Unfortunately, Protect Sheet command can also prevent users from using certain features, such as sorting, formatting changes, aligning text, and many others you might not be concerned with, even when in an unlocked cell.
To overcome this problem, check all or relative options under the Allow all users of this worksheet to:
in the Protect Sheet
dialog box:
The Protect Sheet dialog box has several options, which determine what the user can do when the worksheet is protected:
- Select Locked Cells
Allow users to select locked cells. - Select Unlocked Cells
Allow users to select unlocked cells. - Format Cells
Allow users to apply formatting to cells including locked cells. If you want to prevent applying formatting on locked cells uncheck theSelect Locked Cells
option. - Format Columns and Format Rows
Allow users to hide or change the width/height of columns/rows. You can not format rows/columns if they contain locked cells andSelect Locked Cells
option is unchecked. - And many other self-explanatory options.
Using VBA to Protect Sheet If Locked Cells Found
The last method also will enable you to use all of Excel’s features, but only when you are in a cell that is not locked. To start, ensure that only the cells you want protected are locked and that all other cells are unlocked.
Right-click the Sheet Name tab, select View Code
from the pop-up menu, and enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Locked = True Then Me.Protect Else Me.Unprotect End If End Sub
If a password is used, use Password:="YourPassword
“. Change the word YourPassword
to your password. Press Alt+Q
or ⌘+Q
or click the X in the top right-hand corner to get back to Excel and save your workbook.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Locked = True Then Me.Protect Password:="YourPassword" Else Me.Unprotect Password:="YourPassword" End If End Sub
Now, each time you select a cell that is locked, your worksheet will automatically protect itself. The moment you select any cell that is not locked, your worksheet will unprotect itself.
This code doesn’t work perfectly, though it usually works well enough. The keyword used in the code, Target
, will refer only to the cell that is active at the time of selection. For this reason, it is important to note that if a user selects a range of cells (with the active cell being an unlocked cell), it is possible for him to delete the entire selection because the target cell is unlocked and, therefore, the worksheet automatically will unprotect itself.