Protect or Unprotect All Excel Sheets at Once with VBA

Protect a whole workbook with one click while your macros keep working, using the UserInterfaceOnly option most people miss.

Cover for the guide: Lock and unlock every sheet with one Master Control button

Most of the management systems I build have a Master Control button. Users enter data through forms and cannot break formulas, while the admin can unlock everything with one click to make changes.

The macro

Option Explicit

' One button to lock or unlock every sheet in the workbook
' Free module from sourabsaha.com
'
' Change the password below before you use it.
' UserInterfaceOnly lets your own macros keep editing locked sheets.

Public Const MasterPassword As String = "change-this-password"

Public Sub ToggleMasterControl()
    Dim ws As Worksheet
    Dim locking As Boolean

    locking = Not ThisWorkbook.Worksheets(1).ProtectContents

    For Each ws In ThisWorkbook.Worksheets
        If locking Then
            ws.Protect Password:=MasterPassword, UserInterfaceOnly:=True, AllowFiltering:=True
        Else
            ws.Unprotect Password:=MasterPassword
        End If
    Next ws

    MsgBox IIf(locking, "All sheets are locked.", "All sheets are unlocked for editing."), vbInformation
End Sub

' UserInterfaceOnly is not saved with the file.
' Call this from Workbook_Open in the ThisWorkbook module so macros keep working after reopening.
Public Sub ReapplyProtection()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.ProtectContents Then
            ws.Protect Password:=MasterPassword, UserInterfaceOnly:=True, AllowFiltering:=True
        End If
    Next ws
End Sub

Why UserInterfaceOnly matters

Normally a protected sheet also blocks your own macros, so every macro has to unprotect and protect again. With UserInterfaceOnly:=True the sheet is locked for users but VBA can still write to it.

The catch is that Excel does not save this setting. When the file is reopened, sheets stay protected but macros are blocked again. That is what ReapplyProtection is for. Add this to the ThisWorkbook module:

Private Sub Workbook_Open()
    ReapplyProtection
End Sub

Before you lock

  • Select the input cells users should edit, press Ctrl + 1 and untick Locked on the Protection tab.
  • Change MasterPassword and protect the VBA project too (Tools, VBAProject Properties, Protection), otherwise anyone can read the password.
  • Sheet protection keeps honest users from breaking things. It is not strong security for sensitive data.

Free download

Download Master-Control-Lock.bas

  1. Open your workbook and press Alt + F11 to open the VBA editor.
  2. Choose File, Import File and pick the downloaded .bas file.
  3. Save the workbook as Excel Macro-Enabled Workbook (.xlsm).
  4. Run the macro from Developer, Macros, or assign it to a button.

Common problems and quick fixes

  • Users can still edit some cells. Those cells are unlocked. Select them, press Ctrl + 1, open the Protection tab and tick Locked before you run the lock macro.
  • “The cell or chart is protected” when a macro runs. Your other macros need UserInterfaceOnly. The Master Control macro sets it every time the file opens, because Excel forgets it when the workbook closes.
  • The password is lost. Keep it in one constant at the top of the module, store a copy in your password manager, and never hard-code it in several places.
  • Filtering and sorting stop working. Add AllowFiltering:=True and AllowSorting:=True to the Protect line so people can still use the table.

Frequently asked questions

Is sheet protection real security?

No. It stops mistakes, not determined people. Sheet passwords can be removed with free tools, so keep private data in a separate file that has a file-open password.

Can I lock only some sheets?

Yes. Add an If line inside the loop that skips sheets by name, for example the Input sheet that people still need to fill in every day.

Will this work in a shared OneDrive file?

Macros do not run in Excel for the web. The lock stays in place there, but the Master Control button only works when the file is opened in the desktop app.

Can the button show whether the sheets are locked?

Yes. Change the button caption inside the macro, for example to Locked or Unlocked, so everyone can see the current state at a glance.

Related guides

Need this built for your own workbook? I write protection, approval and audit-trail macros for US businesses every week, tested on a copy of your file and delivered with a short how-to video. See my Excel VBA automation service or get a free quote in under a minute.

12 Excel VBA macros that save hours every week

The small macros I use on almost every client file. One .bas file you import once, with clear comments and step by step instructions.

  • Unhide, protect and unprotect all sheets
  • Delete blank rows and clean messy text
  • Highlight duplicates in any range
  • Split one sheet into many by a column
  • Combine every sheet into one
  • Export each sheet to its own PDF
  • Clickable sheet index, dated backups and a formula audit list

No spam. Your email is never shared. Privacy Policy

Sourab Saha, Data Analyst

About the author

Sourab Saha · Data Analyst · Kolkata, India

I’m Sourab Saha, a data analyst and automation developer from Kolkata, India, with more than ten years of experience turning manual data work into clean, automated systems. On Fiverr and Upwork I have delivered over 500 data and automation projects for clients in the US, UK, Europe, Australia and the Middle East, with a 5.0 rating on both platforms. My toolkit is Excel VBA, SQL, Power BI and DAX, Python with Selenium, and Google Apps Script. I collect data from files, databases and websites, clean it, model it and present it so decisions take minutes instead of days. I build and test every tool on your real data, deliver it with a short walkthrough, and keep revising until it works exactly the way you need. Alongside my freelance work, since 2024 I have also worked as a Data Analyst for AH Group of Industries, a US healthcare group, building automated reports and Power BI dashboards from billing, payroll, audit and attendance data.

Rated 5.0 on Fiverr and Upwork, with more than 500 automation projects delivered and over ten years of Excel VBA. You get a fixed price before any work starts, your files stay private on your order page, and every order comes with a proper invoice.

WhatsApp