Automatically Hide or Show Excel Rows Based on Cell Values
Question details
Automatically hide or show Excel rows according to the value in a control cell for each row.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet uses values in column I to determine whether rows 5 through 26 should be visible.
- Observed behavior
- Rows with the value Required remain visible, while rows with other values are hidden.
Confirm the target worksheet, row range, and control column. This example checks I5:I26 and requires an Excel version that supports VBA macros.
Use a worksheet event to hide and show rows automatically
Use this VBA solution when users directly edit control values in column I.
The macro checks every control cell in I5:I26. It ignores leading and trailing spaces and letter case, so values such as required and Required both keep the row visible.
In Excel, open Developer > Visual Basic. In the Project pane, double-click the worksheet that contains the control cells.
Paste this code into the worksheet's code window: Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Me.Range("I5:I26")) Is Nothing Then UpdateRequiredRows End If End Sub Private Sub UpdateRequiredRows() Dim i As Long Dim cellValue As Variant For i = 5 To 26 cellValue = Me.Cells(i, 9).Value If IsError(cellValue) Then Me.Rows(i).Hidden = True Else Me.Rows(i).Hidden = (LCase$(Trim$(CStr(cellValue))) <> "required") End If Next i End Sub
Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). When reopening it, enable macros only if you trust the workbook.
Edit or re-enter a value in I5:I26 to run the initial check. Change a visible row's control cell to another value to hide it; set a control cell to Required to show its row again.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and continue everyday spreadsheet work with an easy transition from Microsoft Office. VBA support depends on the WPS edition and configuration, so verify macro compatibility before relying on this Excel-specific solution.

Frequently Asked Questions
Why are my rows not updating when a formula result changes?
The Worksheet_Change event responds to direct edits, not formula recalculation. Formula-driven control cells need a different trigger, such as a carefully designed Worksheet_Calculate event.
How do I show all the hidden rows again?
Select the rows surrounding the hidden range, then use Home > Format > Hide & Unhide > Unhide Rows. A later edit to I5:I26 will reapply the macro's rule.
Can I use a different column or row range?
Yes. In the worksheet code, change both I5:I26 and the loop bounds 5 To 26. Change the column number in Cells(i, 9) to match the new control column.
Why doesn't the macro run when I open the workbook?
Save the file as .xlsm and enable macros if you trust it. This event runs after an edit in I5:I26, so re-enter a control value to apply the rule after opening.




