Search code examples
excelvba

Excel macro Hide and unhide row


I wrote a macro that hides row where we have zero cells, but I want to add one more code that will unhide it too. Hide and unhide together. The code is below:

    Sub HideRows()
    Dim cell As Range
    For Each cell In Range("U9:U149")
        If Not IsEmpty(cell) Then
            If cell.Value = 0 Then
                cell.EntireRow.Hidden = True
            End If
        End If
    Next
End Sub

Solution

  • If you mean you want the same code to toggle the rows between hidden / visible, then change it to:

    Sub ToggleHideRows()
        Dim c As Range
        For Each c In Range("U9:U149")
            If Not IsEmpty(c) And c.Value = 0 Then
                c.EntireRow.Hidden = Not c.EntireRow.Hidden
            End If
        Next
    End Sub
    

    I've changed your variable name from cell to c - it's a bad idea to used 'special' words as variable names.