How To Hide Rows In Excel Vba

To hide rows in Excel using VBA, you can use the EntireRow property and set its Hidden attribute to True. This can be done by looping through the rows and checking certain conditions to determine whether to hide the row or not.

Here’s an example:

Sub HideRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    
    ' Set the worksheet you want to work with
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' Determine the last row with data in column A
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each row from 2 to the last row
    For i = 2 To lastRow
        ' Check the condition you want to use to hide the row
        If ws.Cells(i, "A").Value = "Hide" Then
            ws.Rows(i).EntireRow.Hidden = True
        End If
    Next i
End Sub
  

In this example, we have assumed that you want to hide rows where the value in column A is “Hide”. You can modify the condition to suit your specific needs. Simply replace "Hide" with the criteria you want to use.

Make sure to change "Sheet1" to the actual name of the worksheet you want to apply this code to.

Leave a comment