Thursday, September 29, 2011

Hide Rows when cell value matches the given criteria

If you want to hide a row, when the cell value matches any criteria.For Example you have names in col A and you want to hide entire row if cell value is "j".Snapshot Below-



Here is the code-

Example 1:
Sub hide_rows_with_matching_values()
Dim i As Long
For i = Sheets(1).Range("a65356").End(xlUp).Row To 1 Step -1
' it will hide all the rows when name is equal to "J"

' we have taken "1" in Cells(i, 1 ) below because we r looking in row 1
If Sheets(1).Cells(i, 1).Value = "j" Then
Sheets(1).Cells(i, 1).EntireRow.Hidden = True
End If
Next
End Sub


Example 2:
Sub hide_rows_with_greater_than_some_value()
Dim i As Long
For i = Sheets(1).Range("a65356").End(xlUp).Row To 1 Step -1
' it will hide all the rows when value in col b > 30

' we have taken "2" in Cells(i, 2 ) below because we r looking in row 2

If IsNumeric(Sheets(1).Cells(i, 2).Value) And Sheets(1).Cells(i, 2).Value > 30 Then

Sheets(1).Cells(i, 2).EntireRow.Hidden = True
End If
Next
End Sub

No comments:

Post a Comment

Import data from SQL

Macro to import data from SQL using ADO connection string: Sub Import_data_from_SQL() ' Tools -> References -> Microsoft Active...