How to Select and Lock Cells Below Specific Values Using Excel VBA
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.

- 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 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.
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.
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'.
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.
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.
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 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. Download and install WPS Office: Get the latest version of WPS Office from the official website and open WPS Spreadsheet.
- 2. Enable Developer Tools: Navigate to the settings menu to ensure the 'Developer' tab is enabled and visible on your ribbon.
- 3. Open the VBA Editor: Click on the 'Developer' tab and select 'VBA Editor', or press Alt+F11 to access the coding environment.
- 4. Execute your macro: Insert a new module, paste your VBA code for locking the specific cells, and click 'Run' to apply the changes.

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.




