Fix Excel VBA Not Hiding Rows Based on Formula Results
Question details
The user needs a VBA macro to automatically hide or display specific rows (e.g., 18:20) when the value of a target cell (e.g., M10) changes based on a calculated formula.
- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating row visibility based on dynamic formula results in a worksheet.
- Observed behavior
- The VBA macro fails to execute and update the row visibility when the formula calculates a new result, unlike when values are manually typed into the cell.
Ensure that macros are enabled in your Trust Center settings and that your worksheet is not protected, which could block automated row formatting changes.
Use the Worksheet_Calculate Event
The standard Worksheet_Change event does not fire when a formula result updates. Switching to the Worksheet_Calculate event ensures your code runs whenever the sheet recalculates.
When a cell contains a formula, its value updates through background recalculation rather than a physical change event. Therefore, to trigger a macro when cell M10 calculates a new value, you must place your VBA code inside the Worksheet_Calculate event rather than Worksheet_Change.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer panel on the left, double-click the specific worksheet where your formula resides (e.g., Sheet1).
From the top-left drop-down menu in the code window, select 'Worksheet'. From the top-right drop-down, select 'Calculate' to generate the Private Sub Worksheet_Calculate() block.
Insert your row-hiding logic. For example: Rows("18:20").EntireRow.Hidden = (Range("M10").Value = 0). This evaluates the condition and hides the rows if M10 equals zero.
Verify Macro Settings and Worksheet Protection
If the macro still doesn't run after updating the event type, security settings or sheet protection might be blocking execution.
Easily Manage Macros and Automate Workflows with WPS Office
WPS Spreadsheets provides robust support for VBA and macros, allowing you to automate complex tasks like conditionally hiding rows based on cell values just as you would in Microsoft Excel.
- 1. Open Your Workbook: Launch WPS Spreadsheets and open your macro-enabled file.
- 2. Access the VBA Editor: Navigate to the Developer tab and click the 'VBA Editor' icon.
- 3. Implement the Code: Double-click your target worksheet object and paste the Worksheet_Calculate macro script.
- 4. Save and Run: Save your document as an .xlsm file. The rows will now automatically hide or unhide as your formulas recalculate.

Frequently Asked Questions
Why doesn't the Worksheet_Change event work for formulas?
The Worksheet_Change event is triggered exclusively by direct user actions, such as manually typing a value, deleting a cell's contents, or pasting data. It is explicitly designed to ignore changes caused by background formula recalculations.
How do I unhide rows manually if my VBA script fails?
Highlight the rows directly above and below the hidden section (for example, select rows 17 and 21), right-click the row numbers on the left-hand sidebar, and choose 'Unhide' from the context menu.
Can I hide rows automatically based on a cell value without using VBA?
No, Excel and other standard spreadsheet programs do not have a built-in feature to dynamically hide rows based on cell values without relying on VBA macros. As an alternative, you can use Conditional Formatting to make the text blend into the background color, but the physical row will remain visible.




