VBA & Macro Problems

Automatically Hide or Show Excel Rows Based on Cell Values

Bushra ParveenBushra Parveen Dec 2, 2026 868 views

Question details

Automatically hide or show Excel rows according to the value in a control cell for each row.

Automatically Hide or Show Excel Rows Based on Cell Values
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.
Before you start

Confirm the target worksheet, row range, and control column. This example checks I5:I26 and requires an Excel version that supports VBA macros.

Solution 1Recommended

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.

1
Open the worksheet code

In Excel, open Developer > Visual Basic. In the Project pane, double-click the worksheet that contains the control cells.

2
Add the event code

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

3
Save and enable the macro

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.

4
Apply and test the rule

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.

Use a worksheet event to hide and show rows automatically
Formula results need a different trigger: Worksheet_Change runs for direct edits, including pasted values, but not when a formula result changes during recalculation. Adjust the row range, column number, and comparison text in the code if your worksheet uses different controls.
Free Microsoft Office alternative

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.

Open and edit compatible Microsoft Excel XLSX spreadsheetsReview and organize worksheet rows in a familiar interfaceUse a free, lightweight suite for everyday Office tasksMove between compatible Office documents with minimal disruption
microsoft office alternative - wps office

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.