Hide rows, based on value of cell

In Excel

Excel
VBA
Author

Christian Knudsen

Published

March 12, 2018

So - you want to hide some rows in a worksheet, based on the value in a cell. Or more than one cell.

We do that with VBA, here’s how.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Application.WorksheetFunction.CountA(Range("A7:A100000")) + 6
On Error Resume Next
For Each c In Range("A7:A" & LastRow)
If (c.Value = Cells(1, 7).Value And c.Offset(0, 3).Value = Cells(2, 7).Value) Then
c.EntireRow.Hidden = False
Else
c.EntireRow.Hidden = True
End If
Next
On Error GoTo 0
Application.EnableEvents = True
End Sub