VBA & Macro Problems

How to Expand Multiple Excel Row Sections Based on Cell Values Using VBA

Steven ParkerSteven Parker Oct 20, 2026 869 views

Question details

The user needs an Excel VBA macro to independently expand or collapse multiple distinct row sections depending on whether specific header cells contain the value 'YES'.

How to Keep Multiple Excel Row Sections Expanded Based on Cell Values Using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Writing or debugging a VBA script to conditionally show or hide groups of rows (14:16, 18:22, 24:27, 29:32) based on trigger cells (J13, J17, J23, J28) containing a 'YES' value.
Observed behavior
The current macro stops evaluating after the first 'YES' is found, preventing multiple sections from remaining expanded simultaneously.
Before you start

Before modifying the macro, verify that your target trigger cells (J13, J17, J23, and J28) and associated row ranges are exactly aligned in your worksheet, and ensure the Developer tab is enabled in your Excel ribbon.

Solution 1Recommended

Remove 'Exit For' Statements from Your VBA Loop

Modify your VBA loop to prevent it from terminating early, ensuring all target cells are evaluated independently.

When a loop contains an 'Exit For' statement, it immediately stops executing once the first matching condition is met. In this scenario, finding the first 'YES' triggers the exit, leaving the remaining sections un-evaluated and collapsed. Removing these statements allows the code to process every heading independently.

1
Open the Visual Basic Editor

Press 'Alt + F11' while in your Excel workbook to launch the Visual Basic for Applications (VBA) editor.

2
Locate Your Macro Code

In the Project Explorer pane on the left, double-click the specific Module or Sheet (e.g., Sheet1) that contains your row expansion macro.

3
Delete 'Exit For' Commands

Review the loop that iterates through your target cells (J13, J17, J23, and J28). Locate and delete any 'Exit For' lines inside the 'If...Then' blocks that check for the 'YES' value.

4
Save and Test the Script

Press 'Ctrl + S' to save your code, then close the VBA editor. Enter 'YES' into multiple trigger cells in your worksheet to confirm that the corresponding row sections now expand simultaneously.

Remove 'Exit For' Statements from Your VBA Loop
Independent Evaluation Complete: Each trigger cell is now processed on its own during the loop, allowing any combination of row sections to be expanded or collapsed at the same time.
Advanced Spreadsheet Automation

Write and Execute VBA Macros Seamlessly in WPS Office

WPS Spreadsheet provides robust support for VBA macros, allowing you to automate complex tasks like row grouping, expanding, and collapsing natively, ensuring your Excel scripts work effortlessly.

  1. 1. Open Your Workbook in WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet (.xlsm) containing the row sections.
  2. 2. Enable Macros: If prompted by the security warning banner below the ribbon, click 'Enable Macros' to allow your background scripts to run.
  3. 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click the 'Visual Basic' icon to launch the built-in VBA Editor.
  4. 4. Modify and Run Your Code: Remove the 'Exit For' statements from your expansion loop, save the module, and test the automation directly within WPS Spreadsheet.
Full compatibility with Microsoft Excel VBA scripts and .xlsm formatsIntuitive Visual Basic Editor for efficient code debuggingLightweight architecture for fast and responsive macro executionEasily manage row visibility and data structures without rewriting scripts
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Excel macro only expand the first section and ignore the rest?

This behavior typically occurs if your VBA code contains an 'Exit For' or 'Exit Sub' command immediately after identifying the first matching condition (such as finding the first 'YES'). Removing the exit command forces the script to continue checking all remaining sections.

How do I group and hide rows in Excel VBA without using a loop?

Instead of looping through cells, you can directly target row ranges in your code. For instance, using `Rows("14:16").Hidden = True` will collapse the section, while setting it to `False` expands it. You can bind these exact statements to individual cell checks.

Can I make the rows expand automatically the moment I type 'YES' into a cell?

Yes, you can achieve this by using the `Worksheet_Change` event in VBA. By placing your expand and collapse logic inside this event block, Excel will automatically evaluate the row visibility as soon as a user modifies the designated target cells.