How to Expand Multiple Excel Row Sections Based on Cell Values Using VBA
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'.

- 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 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.
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.
Press 'Alt + F11' while in your Excel workbook to launch the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, double-click the specific Module or Sheet (e.g., Sheet1) that contains your row expansion macro.
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.
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.

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. Open Your Workbook in WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet (.xlsm) containing the row sections.
- 2. Enable Macros: If prompted by the security warning banner below the ribbon, click 'Enable Macros' to allow your background scripts to run.
- 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. 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.

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.




