Automatically Hide Excel Rows When Column F Contains N/A
Question details
Automatically hide or unhide Excel rows based on whether the value in column F is N/A.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Column F contains formula-driven values that can change to or from N/A.
- Observed behavior
- Rows should be hidden when column F contains N/A and visible when it no longer does.
Open the workbook in a desktop version of Excel that supports VBA, and check whether column F returns the text N/A or the #N/A error. The example below handles both and assumes row 1 is a header.
Use Worksheet Events to Hide and Unhide Rows
Use this VBA solution when column F contains formulas that can change during recalculation.
Worksheet_Calculate checks formula results after recalculation. Worksheet_Change also checks direct edits to column F. The code updates rows 2 through the last used row in column F.
In desktop Excel, open the Visual Basic Editor from Developer > Visual Basic. In the Project pane, double-click the worksheet containing the values in column F.
Paste the following code into that worksheet's code window. If either event already exists there, merge the relevant UpdateRows call into the existing event instead of creating a duplicate: Private Sub Worksheet_Calculate() UpdateRows End Sub Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Me.Columns("F")) Is Nothing Then UpdateRows End If End Sub Private Sub UpdateRows() Dim lastRow As Long Dim c As Range Dim shouldHide As Boolean lastRow = Me.Cells(Me.Rows.Count, "F").End(xlUp).Row If lastRow < 2 Then Exit Sub For Each c In Me.Range("F2:F" & lastRow) shouldHide = False If Application.WorksheetFunction.IsNA(c.Value2) Then shouldHide = True ElseIf VarType(c.Value2) = vbString Then shouldHide = (UCase$(Trim$(c.Value2)) = "N/A") End If c.EntireRow.Hidden = shouldHide Next c End Sub
Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). When reopening the file, enable macros if prompted and if you trust the workbook.
Change a value that affects a formula in column F, or edit a cell in column F directly. Confirm that an N/A or #N/A result hides its row and that another result unhides it. If needed, use Formulas > Calculate Now to trigger recalculation.

Work With Excel Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel file formats, making it easy to move everyday spreadsheet work between applications. It does not fix Excel's VBA event behavior; macro support can vary by WPS version and platform.

Frequently Asked Questions
Why is Worksheet_Calculate needed instead of only Worksheet_Change?
A formula result can change during recalculation without a direct edit to its cell. Worksheet_Calculate checks those results, while Worksheet_Change handles direct edits to column F.
Does the code handle both N/A text and the #N/A error?
Yes. It checks for the #N/A error with the ISNA worksheet function and separately checks for the text N/A, ignoring letter case and surrounding spaces.
Why aren't the rows updating when a formula changes?
Make sure macros are enabled and the code is in the relevant worksheet's code window. Check Formulas > Calculation Options and select Automatic, or use Formulas > Calculate Now.
Will the macro preserve rows I hid manually?
No. Each time it runs, the code sets the visibility of every checked row according to its column F value, so a manually hidden row can be unhidden.
Where should I put the VBA code?
In Developer > Visual Basic, double-click the worksheet that contains column F in the Project pane and paste the code into that worksheet's code window, not a standard module.




