VBA & Macro Problems

How to Select and Lock Cells Below Specific Values Using Excel VBA

Camila MilosovichCamila Milosovich Oct 29, 2026 869 views

Question details

The user wants to use an Excel VBA macro to search for nonadjacent cells containing the value "x", select a specific number of cells immediately below each match, and lock only those target ranges.

How to Select and Lock Cells Below Specific Values Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Automating cell protection for dynamically located target ranges based on the content of trigger cells.
Observed behavior
The user needs a script to lock specific cells but encountered a problem where the entire worksheet became protected while the intended cells remained editable.
Before you start

Before running the VBA macro, ensure your worksheet is initially unprotected and all cells in the sheet are set to 'Unlocked' so the script can accurately apply locks only to the targeted ranges.

Solution 1Recommended

Use VBA to Find, Resize, and Lock Target Cells

Create a VBA macro that unprotects the sheet, searches for the target value, resizes the selection to include the rows below, locks the range, and reprotects the sheet.

To successfully lock specific cells based on a value, you must first unlock all cells on the worksheet. The VBA script will then search for the specified value (like 'x'), use the Resize property to select the cells below it, and change their Locked status to True.

1
Unlock all cells on the worksheet

Press Ctrl+A to select the entire sheet. Right-click anywhere on the selection and choose 'Format Cells'. Go to the 'Protection' tab, uncheck the 'Locked' box, and click 'OK'.

2
Open the VBA Editor

Press Alt+F11 to open the Visual Basic for Applications editor. Go to the 'Insert' menu and select 'Module' to create a new blank script window.

3
Write the search and lock code

Write a script that loops through the target range using the .Find method to locate the exact lowercase 'x'. For each found cell, use the .Resize property (e.g., .Resize(6)) to expand the range downward, and apply Selection.Locked = True.

4
Enforce worksheet protection

Cell locking only takes effect when the sheet is protected. Add ActiveSheet.Protect at the end of your VBA code, or manually go to the 'Review' tab in the ribbon and click 'Protect Sheet'.

Use VBA to Find, Resize, and Lock Target Cells
Adjusting the Range Size: The .Resize(n) parameter controls the number of rows to lock. If you want to lock the cell containing 'x' and the 9 cells below it, change it to .Resize(10). Ensure the cells containing 'x' do not have leading or trailing spaces.
Advanced Spreadsheet Macros

Use WPS Spreadsheet for Powerful VBA Macro Support

WPS Office provides robust support for VBA macros, allowing you to easily automate repetitive tasks like locking specific cells based on conditions. It offers a familiar developer interface and excellent compatibility with macro-enabled files.

  1. 1. Download and install WPS Office: Get the latest version of WPS Office from the official website and open WPS Spreadsheet.
  2. 2. Enable Developer Tools: Navigate to the settings menu to ensure the 'Developer' tab is enabled and visible on your ribbon.
  3. 3. Open the VBA Editor: Click on the 'Developer' tab and select 'VBA Editor', or press Alt+F11 to access the coding environment.
  4. 4. Execute your macro: Insert a new module, paste your VBA code for locking the specific cells, and click 'Run' to apply the changes.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled formatsBuilt-in VBA editor for writing, editing, and debugging codeLightweight application with fast script processing speedsFree and easy-to-use alternative with a familiar ribbon interface
QA img-9

Frequently Asked Questions

Why are my targeted cells still editable after running the VBA macro?

If cells are still editable, the worksheet itself might not be protected. Setting a cell's Locked property to True only takes effect when the worksheet protection is active. Ensure you use ActiveSheet.Protect in your code or click 'Protect Sheet' from the Review tab.

How do I change the number of cells locked below the target value?

You can adjust the .Resize(n) property in your VBA code. For example, changing .Resize(6) to .Resize(10) will lock the target cell and the 9 cells directly below it.

Why isn't the VBA macro finding my 'x' values?

The .Find method in VBA might be searching for an exact match. Ensure that the cells contain only a lowercase 'x' without hidden spaces or additional text. You may also need to check the LookAt:=xlWhole parameter in your search function.