How to Automatically Hide Excel Rows When Formulas Return Zero (VBA Guide)
Question details
The user is looking for a way to use VBA to automatically hide or unhide specific rows in an Excel worksheet when the underlying formula calculates a zero value or results in a blank cell.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating dynamic reports where rows containing zeros or blanks from formula results need to be hidden dynamically without the need for manual filtering.
- Observed behavior
- The user lacks the specific VBA code and worksheet event logic required to evaluate changing monthly ranges and automatically hide the corresponding rows.
Before applying VBA macros, ensure you save a backup of your workbook, as macro actions cannot be undone using the standard Undo feature. Additionally, ensure the Developer tab is enabled in your Excel ribbon.
Use a Worksheet_Calculate Event Macro
Implement a VBA macro that triggers automatically whenever formulas recalculate, checking a specific range for zero values and hiding the rows.
Because formulas update automatically, you must use the Worksheet_Calculate event rather than a standard module macro. This ensures rows are hidden dynamically when upstream data changes in your spreadsheet.
Right-click the sheet tab at the bottom of your screen that contains your formulas and select 'View Code'.
In the VBA editor, select 'Worksheet' from the left dropdown menu and 'Calculate' from the right dropdown menu to generate the event shell.
Insert a loop to iterate through your target range. For example: For Each cell In Range("B2:B50").
Add an IF statement to check the cell value. If the cell equals 0, set cell.EntireRow.Hidden = True, otherwise set it to False to unhide.
Close the VBA editor and change a value in your sheet that affects the formulas to trigger the automatic recalculation and row hiding.

Seek Custom VBA Support in Microsoft Communities
For highly variable ranges, complex conditions, or cross-sheet dependencies, requesting custom code from an expert is the most effective approach.
Use WPS Spreadsheet for Seamless Macro Execution
WPS Office provides comprehensive support for VBA macros, allowing you to run, edit, and create automation scripts just like in Microsoft Excel. You can easily apply and execute row-hiding macros in WPS Spreadsheet.
- 1. Open your File: Launch WPS Spreadsheet and open your macro-enabled (.xlsm) workbook.
- 2. Access the Developer Tools: Click on the 'Developer' tab in the top ribbon and select 'Visual Basic' to open the built-in VBA editor.
- 3. Paste and Run Code: Paste your Worksheet_Calculate code into the corresponding sheet module, save your work, and watch the rows hide automatically.

Frequently Asked Questions
Can I hide rows with zero values without using VBA?
Yes, if you only want to hide the display of the zeros but keep the row physically visible, you can apply the custom number format `0;-0;;@` or uncheck 'Show a zero in cells that have zero value' in Excel Options. However, completely collapsing the row automatically requires VBA or manual filtering.
Why is my Worksheet_Calculate macro slowing down my Excel file?
Looping through hundreds of rows every time a formula calculates can consume significant processing power. To fix this, restrict your VBA loop to a very specific range rather than entire columns, and add `Application.ScreenUpdating = False` at the beginning of your script.
How do I ensure rows automatically unhide if the formula result changes to a non-zero number?
Ensure your VBA script includes an 'Else' statement for your condition. For example: `If cell.Value = 0 Then cell.EntireRow.Hidden = True Else cell.EntireRow.Hidden = False`. This ensures that rows can reappear when the data conditions are no longer met.




