How To Hide Rows In Excel Using Vba

In order to hide rows in Excel using VBA, you can utilize the “Row.Hidden” property. This property allows you to hide or unhide rows programmatically.

Here is an example of how to hide rows based on specific criteria:

Sub HideRowsBasedOnCriteria()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your desired worksheet name
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Assuming your data is in column A
    
    Dim i As Long
    For i = 1 To lastRow
        If ws.Cells(i, 1).Value = "Criteria" Then ' Replace "Criteria" with your desired condition
            ws.Rows(i).Hidden = True
        End If
    Next i
End Sub

In this example, all rows in column A with the value “Criteria” will be hidden. You can customize the criteria to suit your needs.

To execute this code, you can press “Alt + F11” to open the Visual Basic Editor in Excel, then insert a new module and paste the code. After that, you can run the “HideRowsBasedOnCriteria” macro from Excel by pressing “Alt + F8” and selecting the macro.

Remember to replace “Sheet1” and “Criteria” with your actual worksheet name and condition.

Read more

Leave a comment