VBA & Macro Problems

How to Lock or Unlock Excel Rows Based on a Yes/No Condition Using VBA

Rana GarciaRana Garcia Nov 2, 2026 869 views

Question details

The user needs to dynamically lock specific cells (e.g., B2:D2) when a target cell (e.g., A2) contains 'Yes', and unlock them when it contains 'No', but their current VBA macro is failing to execute properly.

How to Lock or Unlock Excel Rows Based on a Yes/No Condition
Product
Microsoft Excel
Device & OS
not provided
Scenario
Creating a controlled data entry form or restricted worksheet where the editing permissions of certain cells are dictated by the value of a specific condition cell.
Observed behavior
The applied VBA macro does not work as expected, failing to update the locked property of the target cells or resulting in an error due to worksheet protection conflicts.
Before you start

Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled macros in your Excel or WPS Spreadsheet security settings before running the code.

Solution 1Recommended

Use a Standard VBA Macro to Update Cell Lock Status

Run a specific macro that temporarily unprotects the sheet, evaluates each row for the 'Yes' or 'No' condition, updates the cell lock properties, and then reprotects the sheet.

When applying a VBA macro to change cell locks, it is crucial to place the code in the correct module and ensure the sheet is unprotected before the code modifies cell properties. If a sheet is protected, VBA cannot change the 'Locked' status of cells unless explicitly instructed to unprotect it first.

1
Open the VBA Editor

Open your workbook and press ALT + F11 to launch the Visual Basic for Applications (VBA) Editor.

2
Insert a Standard Module

In the top menu, click Insert > Module to create a new standard module where the code will be placed.

3
Paste the VBA Code

Copy and paste the following code into the module window: Sub UpdateLockCondition() Dim R As Range ActiveSheet.Unprotect For Each R In Range("A2", Range("A" & Rows.Count).End(xlUp)) Intersect(Range("B:D"), R.EntireRow).Locked = R = "Yes" Next ActiveSheet.Protect End Sub

4
Run the Macro

Close the VBA editor and return to your worksheet. Press ALT + F8, select UpdateLockCondition from the Macro dialog box, and click Run.

Use a Standard VBA Macro to Update Cell Lock Status
Code Placement Matters: If you paste this code into a standard module, it runs on the currently active worksheet. If you place it into a specific Worksheet module, it will only apply to that sheet.
Advanced Spreadsheet Features

Manage Macros and Cell Protection with WPS Spreadsheet

WPS Office fully supports VBA macros, allowing you to run conditional locking scripts just like in Microsoft Excel. You can seamlessly manage worksheet protection and automate your workflows in a free, lightweight environment.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing your conditional data.
  2. 2. Enable Macros: If prompted by a security warning at the top of the screen, click 'Enable Macros' to allow the VBA code to execute.
  3. 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If it is hidden, enable it in the settings, then click 'Visual Basic' or press ALT + F11.
  4. 4. Run Your Macro: Click 'Macros' in the Developer tab, select your locking macro from the list, and click 'Run' to apply the cell protection.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) macro formats.Built-in VBA editor to write, debug, and run custom macros effortlessly.Advanced sheet protection features to prevent unauthorized edits.Free, lightweight, and user-friendly interface for seamless migration.
QA img-9

Frequently Asked Questions

Why do I get a run-time error when the macro tries to lock the cells?

This usually happens because the worksheet is already protected. Your VBA code must include the line ActiveSheet.Unprotect before attempting to modify any cell properties, and ActiveSheet.Protect at the end to re-secure the worksheet.

How can I apply this macro to a different column range instead of B:D?

In the VBA code line that reads Intersect(Range("B:D"), R.EntireRow), simply change "B:D" to your desired column range, such as "E:G" or "C:F".

Does WPS Spreadsheet support this exact VBA code?

Yes, WPS Spreadsheet supports VBA. You can use the exact same code to conditionally lock and unlock cells, provided the VBA module is installed and enabled in your version of WPS Office.