VBA Macro to Hide rows depending on Column Value.

Macro to hide rows if the column is blank:


Sub HideRows()
    BeginRow = 1 'Starting row number
    EndRow = 18 ' Ending row number
    ChkCol = 2 'First Column to check for blank values
    ChkCol = 3 'Second Column to check for blank values

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
End Sub

For checking only single column for a blank value:


Sub HideRows()
    BeginRow = 1 'Starting row number
    EndRow = 18 ' Ending row number
    ChkCol = 2 'Column to check for blank values

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
End Sub

Macro to hide and unhide rows checking the contents of a cell that changes:


You should note that the above macro doesn't unhide any rows, it simply hides them. If you are checking the contents of a cell that can change, you may want to modify the macro a bit so that it will either hide or unhide a row, as necessary. The following changes in the macro will do the trick:

Sub HideRows()
    BeginRow = 1 'Starting row number
    EndRow = 100 ' Ending row number
    ChkCol = 3 'Column to check for blank values

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
End Sub


If you want to include a condition like "less than(<)" and "Greater than(>)" then you can edit the values in the following code of the above macro:

If Cells(RowCnt, ChkCol).Value = "" Then
or
If Cells(RowCnt, ChkCol).Value < 5 Then
or
If Cells(RowCnt, ChkCol).Value > 5 Then

No comments:

Post a Comment

Thank You.

https://linwintech.blogspot.com/