To protect a specific range in Excel with VBA, you unlock all cells, lock only the cells you want to protect, then enable sheet protection with a password. The key point: cell locking only takes effect once sheet protection is turned on, and by default every cell is locked. So the reliable sequence is unlock everything first, lock the target range, then protect the sheet. This guide walks through that with VBA, plus the mistakes that make protection appear to do nothing.
What You'll Need
- Excel on Windows or Mac with macros enabled (a .xlsm workbook).
- Basic familiarity with the VBA editor: press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open it.
- The address of the range you want to protect, for example
B2:D20. - A password you are willing to store in code, or a plan to prompt for it at runtime.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11, then in the Project Explorer right-click your workbook and choose Insert > Module. Menu labels can differ slightly by Excel version and platform, but the VBA editor layout is broadly the same.
- Unlock every cell on the sheet. Protection only applies to cells whose Locked property is True. Because all cells start locked, unlock the whole sheet first so only your chosen range ends up locked:
Sub ProtectRange() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") ' 1. Unlock all cells ws.Cells.Locked = False - Lock only the target range. Set Locked to True on just the cells you want protected:
If you want to protect several non-adjacent blocks, repeat this line for each address, or use' 2. Lock the specific range ws.Range("B2:D20").Locked = TrueUnion. - Enable sheet protection with a password. Locking alone does nothing until the sheet is protected:
The password is case-sensitive. Store it somewhere safe; if you lose it, recovering access is difficult.' 3. Protect the sheet ws.Protect Password:="YourPassword", _ DrawingObjects:=True, _ Contents:=True, _ Scenarios:=True End Sub - Run the macro. Press F5 with the cursor inside the subroutine, or use Run > Run Sub/UserForm. Return to Excel and try typing into a locked cell: you should get the standard protection warning, while unlocked cells remain editable.
- Unprotect when you need to edit. Use a companion macro to remove protection before making changes:
Run it, edit, then runSub UnprotectSheet() ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="YourPassword" End SubProtectRangeagain. - Optionally allow specific actions while protected. The
Protectmethod accepts arguments such asAllowFormattingCells:=TrueorAllowFiltering:=True. Add only the ones you need; leaving them out keeps the sheet fully locked apart from unlocked cells.
Common Mistakes
- Locking cells without protecting the sheet. The Locked property is only enforced when protection is on. If users can still edit, you skipped the
Protectcall. - Forgetting to unlock everything first. Because all cells default to locked, protecting the sheet without unlocking first locks the entire sheet, not just your range.
- Protecting the wrong sheet.
ActiveSheetdepends on what is selected when the macro runs. Reference the sheet by name, as in the example, to avoid surprises. - Hard-coding a password in a shared workbook. Anyone who can open the VBA editor can read it. For shared files, prompt for the password with
InputBoxinstead of storing it. - Assuming protection is security. Sheet protection stops accidental edits and casual changes; it is not encryption and can be bypassed by someone determined. Use workbook-level encryption for sensitive data.
Tips
- Name your range (Formulas > Define Name) and lock it by name, e.g.
ws.Range("InputArea").Locked = True. The macro keeps working if rows are inserted. - Set
ws.EnableSelection = xlUnlockedCellsbefore protecting so the cursor can only land on editable cells, which makes data entry smoother. - Combine lock and protect in one macro so users cannot leave the sheet half-configured.
- Test on a copy of the workbook first. Protection changes are easy to reverse with the unprotect macro, but a lost password is not.
- If the workbook is shared, note that some protection options behave differently; check Excel's own help for shared-workbook limitations.
Troubleshooting
If the macro runs without error but cells are still editable, check that ws.Protect actually executed and that you are testing the same sheet the macro targeted. If you get a "method failed" error, the sheet may already be protected; call Unprotect first (with the correct password) before re-protecting. If the wrong cells are locked, confirm you unlocked ws.Cells before setting the target range, and that no other macro re-locks cells afterwards. If the password prompt rejects your password, remember it is case-sensitive and check for stray spaces in the string. On Mac, some dialog labels differ, but the VBA methods are the same. If protection seems to vanish after closing and reopening, make sure you saved the workbook as .xlsm and that the macro ran on the saved file, not an unsaved copy.
Frequently Asked Questions
Can I protect a range without protecting the whole sheet?
No. Excel enforces cell locking only at sheet level, so you protect the sheet but leave the cells outside your range unlocked so they stay editable.
Does the password in VBA encrypt the file?
No. It only controls sheet protection. For real security, use File > Info > Protect Workbook > Encrypt with Password.
Why do all cells lock when I run my macro?
You protected the sheet before unlocking the cells. Unlock ws.Cells first, then lock only the target range, then protect.
Can users still copy the protected range?
By default, yes, unless you set AllowFiltering or other options and design the sheet accordingly. Protection prevents edits, not copying.
What happens if I forget the password?
There is no built-in recovery. Keep the password recorded securely, or prompt for it at runtime so it is not stored in code.
Does this work on Excel for Mac?
Yes, the VBA methods are the same. Only the editor shortcut and some dialog wording differ.