Excel VBA Protect All Sheets in a Workbook
In this article
To protect every worksheet in an Excel workbook with VBA, loop through the workbook’s worksheets and call each sheet’s Protect method. Save a copy before running the macro, because VBA changes cannot be undone with Ctrl+Z. The example below protects all worksheets in the workbook containing the macro.
VBA macro to protect every worksheet
In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste this code into the module. The optional password makes users enter a password to unprotect a sheet through Excel; replace the sample text with your own password, or use an empty string if you do not want password protection.
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim sheetPassword As String
' Replace this example with your chosen password.
sheetPassword = "ChangeThisPassword"
' Protect every worksheet in the workbook containing this code.
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:=sheetPassword
Next ws
End Sub
Return to Excel, press Alt+F8, select ProtectAllSheets, and click Run. Alternatively, run the macro from the Visual Basic Editor with the cursor inside it by pressing F5. On a Mac, open the Visual Basic Editor with Option+F11 (some keyboards require Fn as well), or use Tools > Macro > Visual Basic Editor; keyboard shortcuts can vary by keyboard and Excel settings. The VBA code itself works in desktop Excel for Windows and Mac, but Excel for the web cannot run VBA macros.
What the macro protects—and what it does not
The loop visits objects in ThisWorkbook.Worksheets, which includes visible, hidden, and very hidden worksheets. It does not protect chart sheets, because those are not worksheets. To cover chart sheets too, a separate loop can use the workbook’s Sheets collection, but the protection method and available options differ by sheet type.
Worksheet protection mainly restricts editing of cells and certain worksheet features. It is not the same as encrypting a workbook, protecting its structure, or securing its contents against a determined person. Microsoft describes worksheet protection as a way to prevent changes to locked cells, not as a security feature for sensitive data. Use file encryption and appropriate access controls when confidentiality matters.
By default, cells are marked locked, but that setting has no effect until the sheet is protected. If users should edit specific cells, select those cells first, press Ctrl+1, open the Protection tab, clear Locked, and then protect the sheet. On Mac, use Command+1 to open Format Cells. Users can then edit unlocked cells while other locked cells remain protected.
Choose what users can do on protected sheets
The basic call applies Excel’s default protection options. For example, users generally cannot edit locked cells, insert or delete rows, or format cells. You can allow selected actions by passing named arguments to Protect. This example allows filtering and sorting while keeping the sheet protected:
Sub ProtectSheetsAllowFiltersAndSort()
Dim ws As Worksheet
Dim sheetPassword As String
sheetPassword = "ChangeThisPassword"
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:=sheetPassword, _
AllowFiltering:=True, AllowSorting:=True
Next ws
End Sub
These permissions do not override every restriction. For example, sorting a protected range may be unavailable if the range includes locked cells, and users may need an existing AutoFilter to use filtering. Decide which tasks people need to perform, configure and test those options on a sample sheet, then apply the macro to the workbook. Other useful protection options include allowing formatting cells, columns, or rows, and using UserInterfaceOnly:=True when macros should be able to change protected cells while users remain restricted. That setting may need to be reapplied when the workbook opens, so it is not a permanent saved permission.
Protect the active workbook or a different workbook
ThisWorkbook means the workbook that contains the running VBA code. It is usually the right choice when the macro is stored in the workbook being protected. If the macro is in your Personal Macro Workbook or another file and should act on the currently selected workbook, use ActiveWorkbook instead:
Sub ProtectActiveWorkbookSheets()
Dim targetBook As Workbook
Dim ws As Worksheet
Dim sheetPassword As String
sheetPassword = "ChangeThisPassword"
Set targetBook = ActiveWorkbook
For Each ws In targetBook.Worksheets
ws.Protect Password:=sheetPassword
Next ws
End Sub
Check which workbook is active before running this version. If the Visual Basic Editor or another workbook is in focus, ActiveWorkbook may not be the file you intended. A macro stored in an add-in can also use an explicitly assigned workbook reference to avoid acting on the wrong file.
Passwords, repeat runs, and common errors
A password passed to Protect is required to remove protection through Excel’s normal interface. It is not a substitute for strong file encryption, and worksheet passwords should not be treated as a secure way to hide confidential information. Keep the password somewhere appropriate; if it is lost, you may be unable to unprotect sheets using the normal workflow.
Running the basic macro again on already protected sheets can produce an error if the supplied password does not match the password already on a sheet. If all sheets use the same known password, unprotect each one first, then protect it again with the intended settings. Do not put a password in code if people who can inspect the VBA project must not see it. VBA projects can be protected against casual viewing, but that is not strong encryption.
If Excel reports that macros are disabled, check the file’s security prompt and Trust Center settings rather than lowering security indiscriminately. A workbook containing macros should be saved in a macro-enabled format such as .xlsm; saving it as .xlsx removes VBA code. Always work on a copy when you are uncertain about the workbook’s current protection state.
Protect workbook structure separately
Worksheet protection does not stop someone from renaming, moving, deleting, hiding, or adding sheets. To restrict those workbook-structure changes, protect the workbook structure as a separate step. In Excel for Windows, use Review > Protect Workbook, select Structure if prompted, enter and confirm a password, and save the file. The VBA equivalent is:
Sub ProtectWorkbookStructure()
Dim structurePassword As String
structurePassword = "ChangeThisPassword"
ThisWorkbook.Protect Password:=structurePassword, Structure:=True
End Sub
Workbook structure protection and worksheet protection are independent. A user may be prevented from editing cells yet still be able to change sheet tabs if structure protection is not enabled. Conversely, structure protection does not lock cells on individual sheets. Use each safeguard for the specific changes you want to prevent.
Frequently Asked Questions
Does the macro protect hidden worksheets too?
Yes. The Worksheets collection includes hidden and very hidden worksheets, so the loop protects them along with visible worksheets.
Can I protect every sheet without a password?
Yes. Set sheetPassword to an empty string, or omit the Password argument. Protection without a password is easier to remove, so it is mainly useful for preventing accidental edits rather than controlling access.
How do I unprotect all sheets with VBA?
Loop through ThisWorkbook.Worksheets and call Unprotect with the password used to protect each sheet. If sheets have different passwords, the code must supply the correct password for each one; a single password will not automatically remove protection from sheets protected differently.
Does protecting all sheets also protect the workbook structure?
No. Use the workbook’s structure protection separately if you want to restrict adding, deleting, hiding, moving, or renaming sheets.
Can I run this VBA macro in Excel for the web?
No. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros. Open the workbook in desktop Excel to run the code.