VBA & Macro Problems

How to Protect and Unprotect an Excel Worksheet in VBA

Khadija KhanKhadija Khan Oct 22, 2026 870 views

Question details

The user needs to execute a macro via a command button to insert rows and copy formulas on a protected worksheet without triggering an error.

How to Protect and Unprotect an Excel Worksheet in VBA
Product
Excel
Device & OS
not provided
Scenario
Running automated worksheet modifications via a VBA command button on a sheet that is locked to prevent manual user edits.
Observed behavior
When the macro attempts to insert a row or copy a formula on the protected sheet, it fails. The sheet must be programmatically unprotected before the action and re-protected immediately after.
Before you start

Ensure the Developer tab is enabled in your spreadsheet program and that you have saved your workbook as a Macro-Enabled Workbook (.xlsm) to allow VBA execution.

Solution 1Recommended

Temporarily Unprotect and Reprotect the Sheet in the Button Procedure

The most effective method is to add code that lifts the worksheet protection at the beginning of your macro and restores it right before the macro finishes.

By using the Unprotect and Protect methods within the same VBA subroutine, you ensure that the sheet is only vulnerable for the fraction of a second it takes the macro to run.

If you are executing the code from the worksheet module itself, you can use the 'Me' keyword. Otherwise, you should reference the specific worksheet name.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor, and locate the worksheet module containing your Command Button's click event code.

2
Add the Unprotect command

At the very beginning of your procedure, type 'Me.Unprotect' (or 'Worksheets("Sheet1").Unprotect' if working from a standard module). If the sheet has a password, include it like this: Me.Unprotect Password:="YourPassword".

3
Insert your modification code

Place your standard VBA code for inserting rows and autofilling formulas immediately below the Unprotect line.

4
Add the Protect command

At the end of your procedure before 'End Sub', type 'Me.Protect' (or 'Worksheets("Sheet1").Protect'). Include the password argument again if needed.

Temporarily Unprotect and Reprotect the Sheet in the Button Procedure
Handling Passwords in VBA: Because plain-text passwords in your macro can be read by anyone accessing the VBA editor, it is highly recommended to protect your VBA Project with a separate password.
Powerful Spreadsheet Editor

Easily Manage VBA Macros and Protect Worksheets in WPS Office

WPS Spreadsheet fully supports VBA macros, providing a seamless environment to write code for protecting and unprotecting sheets, inserting rows, and automating complex tasks without any compatibility issues.

  1. 1. Enable the Developer Tab: Open your workbook in WPS Spreadsheet, go to the options, and ensure the Developer tools are enabled to access VBA features.
  2. 2. Access the VBA Editor: Click on the Visual Basic icon in the Developer tab or simply press Alt+F11 to open the code editor.
  3. 3. Write and Execute Code: Enter your Protect and Unprotect code seamlessly. WPS Office will execute your macros exactly as expected to automate your daily tasks.
Full compatibility with Microsoft Excel VBA macros and file formats (.xlsm)Protect worksheets and lock cells with customized passwords easilyFamiliar Developer tab and Visual Basic Editor interfaceFree and lightweight suite for all your document automation needs
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a Run-time error 1004 when running my VBA macro?

This error commonly occurs when a VBA macro attempts to modify cells, insert rows, or copy formulas on a worksheet that is currently protected. You must either unprotect the sheet via code before making changes or use the UserInterfaceOnly property.

Does VBA code require a password argument if the sheet isn't password-protected?

No. If you protected the sheet without assigning a password, simply using 'Worksheets("Sheet1").Unprotect' and 'Worksheets("Sheet1").Protect' without any additional arguments is sufficient.

Can I unprotect all worksheets in a workbook at once using VBA?

Yes, you can loop through all the sheets in your workbook using a 'For Each ws In ActiveWorkbook.Worksheets' loop in VBA, executing the 'ws.Unprotect' method on each sheet sequentially.

Is the VBA password exposed if I share the file?

Yes, if you write the password in plain text in your macro, anyone who can open the VBA code editor can see it. To prevent this, you should lock the VBA Project itself with a password by going to Tools > VBAProject Properties > Protection in the VBA Editor.