Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Protect a Workbook with a Macro

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can protect a workbook from VBA by setting the ProtectStructure and ProtectWindows properties on the ThisWorkbook object, or by calling ThisWorkbook.Protect with a password. The shortest working line is ThisWorkbook.Protect Password:="yourpassword", Structure:=True, Windows:=True. That single statement locks the workbook so sheets cannot be added, deleted, renamed, moved, hidden, or unhidden, and it locks the window layout. It does not protect the contents of individual worksheets — that is a separate step covered below.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled) if you want the macro to persist.
  • A password you will remember — Excel cannot recover a lost workbook password.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 on Windows, or Fn+Option+F11 on Mac. If the Developer tab is visible, you can also click Developer > Visual Basic.
  2. Insert a standard module. In the VBE, choose Insert > Module. A new module appears under Modules in the Project Explorer. You can also put the code in ThisWorkbook, but a standard module is easier to reuse and run manually.
  3. Paste the protection macro. Use this code, replacing the placeholder password:
    Sub ProtectMyWorkbook()
        ThisWorkbook.Protect Password:="ChangeMe123", Structure:=True, Windows:=True
    End Sub
    The Structure argument blocks adding, deleting, renaming, moving, hiding, and unhiding sheets. The Windows argument freezes the window size and position. Both default to True if omitted, so you can shorten the line to ThisWorkbook.Protect "ChangeMe123".
  4. Run the macro. Click anywhere inside the subroutine and press F5, or use Run > Run Sub/UserForm. Alternatively, close the VBE, press Alt+F8, select ProtectMyWorkbook, and click Run.
  5. Confirm the protection took effect. Return to Excel and try right-clicking a sheet tab. The Insert, Delete, Rename, Move or Copy, and Hide options should be greyed out. The workbook structure is now locked.
  6. Protect individual worksheets too, if needed. Workbook protection does not stop edits inside cells. To lock cell contents, add a second line to the same macro: ThisWorkbook.Worksheets("Sheet1").Protect Password:="ChangeMe123". Repeat for each sheet, or loop through ThisWorkbook.Worksheets to protect them all.
  7. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save will strip the macro on reopen.
  8. Create an unprotect macro for your own use. Add a companion subroutine: ThisWorkbook.Unprotect Password:="ChangeMe123". Keep this in a separate module or a personal macro workbook so you are not locked out of your own file.

Common Mistakes

  • Confusing workbook protection with worksheet protection. Workbook protection stops structural changes only. If users can still type in cells, you protected the wrong object.
  • Hard-coding the password in a visible module. Anyone who opens the VBA editor can read it. If the project itself is not password-protected, the workbook password offers little secrecy.
  • Forgetting to save as .xlsm. The macro disappears the next time the file opens if it was saved as .xlsx.
  • Protecting before the workbook is finalised. If you protect structure and then need to add a sheet, you must unprotect first. Build the workbook fully, then protect.
  • Assuming Protect blocks closing or saving. It does not. Users can still close, save, and reopen the file; they simply cannot restructure it.

Tips

  • Use ThisWorkbook.Protect rather than ActiveWorkbook.Protect so the macro always targets the file it lives in, not whichever workbook happens to be active.
  • Store the password in a cell on a hidden, very-hidden sheet and read it with ThisWorkbook.Sheets("Config").Range("A1").Value if you want to avoid editing code each time. Very-hidden sheets require VBA to unhide, which adds a small layer of obscurity.
  • Add On Error Resume Next before the Protect line if the macro may run on an already-protected workbook; otherwise it raises a run-time error.
  • Use a Workbook_Open event in ThisWorkbook to protect automatically each time the file opens, and Workbook_BeforeClose to unprotect and re-protect cleanly.
  • Password-protect the VBA project itself (Tools > VBAProject Properties > Protection) so casual users cannot read your code. This is not strong encryption and is not a substitute for real file security.

Troubleshooting

  • Run-time error 1004 “The password you supplied is not correct.” The workbook is already protected with a different password. Unprotect it first with the correct one, or use ThisWorkbook.Unprotect with the original password.
  • The macro does nothing. Check that the code is in a standard module, not a sheet module, and that macros are enabled in the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings).
  • Sheet tabs are still editable. You protected the workbook, not the sheets. Add worksheet-level Protect calls.
  • The macro vanished after saving. The file was saved as .xlsx. Save As .xlsm and re-add the code.
  • You are locked out. Excel has no built-in password recovery. Restore from a backup or a previous version if one exists. This is why keeping the password in a safe place matters.

Frequently Asked Questions

Does workbook protection stop people editing cells?

No. It only blocks structural changes such as adding, deleting, renaming, moving, or hiding sheets, and locks the window layout. Cell editing is controlled by worksheet protection.

Can I protect a workbook without a password?

Yes. Call ThisWorkbook.Protect with no arguments. Anyone can then unprotect it from the Review tab, so this only discourages accidental changes, not deliberate ones.

What is the difference between Structure and Windows?

Structure locks sheet organisation. Windows locks the size and position of workbook windows. Both default to True, so omitting them protects both.

Will the macro run automatically when the file opens?

Only if you place the Protect call inside the Workbook_Open event in the ThisWorkbook module, and only if macros are enabled on that machine.

Can I unprotect the workbook from VBA if I forgot the password?

No. VBA requires the correct password to unprotect. There is no supported bypass, and third-party “password recovery” tools are unreliable and often unsafe.

Does protecting the workbook encrypt the file?

No. Workbook protection is not encryption. To encrypt, use File > Info > Protect Workbook > Encrypt with Password, which is a separate feature.