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 SubWhy 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 SubBefore you lock
- Select the input cells users should edit, press Ctrl + 1 and untick Locked on the Protection tab.
- Change
MasterPasswordand 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
- Open your workbook and press Alt + F11 to open the VBA editor.
- Choose File, Import File and pick the downloaded .bas file.
- Save the workbook as Excel Macro-Enabled Workbook (.xlsm).
- 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
- Send personalised emails from Excel with Outlook
- Combine all CSV files in a folder into one sheet
- Split one sheet into many sheets by column value
- 12 Excel VBA macros that save hours every week
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.
