How to Protect and Unprotect an Excel Worksheet in VBA
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.

- 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.
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.
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.
Press Alt + F11 to open the Visual Basic for Applications editor, and locate the worksheet module containing your Command Button's click event code.
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".
Place your standard VBA code for inserting rows and autofilling formulas immediately below the Unprotect line.
At the end of your procedure before 'End Sub', type 'Me.Protect' (or 'Worksheets("Sheet1").Protect'). Include the password argument again if needed.

Use the UserInterfaceOnly Protection Argument
You can protect the worksheet in a way that blocks manual user edits but permits VBA macros to make changes without needing to unprotect the sheet every time.
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. 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. 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. 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.

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.




