Description:

We are required to Hide-UnHide the rows in some types of requirements. For examples we may have data for 3 different categories of items and we may want to show the respective items based on selection. In this case we can achieve by Hiding or Un-hiding the Rows. We will see this in the following examples:

Hide UnHide Rows in Excel Worksheet using VBA – Solution(s):

You can use EntireRow.Hidden property of Row. If you set hidden property TRUE, it will hide the rows. Or if you set it to FALSE then it will make rows to visible.

Hide-UnHide Rows in Excel Worksheet using VBA – An Example

The following example will show you how to Hide and Unhide the rows in excel worksheet using VBA. We can Hide or Unhide the multiple rows at a time. In this example I am hiding and Unhiding Rows 5 to 8.

Code:
Sub sbHidingUnHideRows()
'To Hide Rows 22 to 25
Rows("5:8").EntireRow.Hidden = True
'To UnHide Rows 22 to 25
Rows("5:8").EntireRow.Hidden = False
End Sub
Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module for Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to run it
Output:

Here is the example screen-shot. You can download the file below and see the code. Hide button will call the procedure to hide the rows and unhide button will unhide the rows. For example, Food Button will show you the data and charts related to food. And ‘Show All’ button will show you all categories and ‘Hide All’ button will hide all categories.

Hide Unhide Rows Examples 1

Hide-UnHide Rows in Excel Worksheet using VBA – Case study

Here is the example case study. You can download the file below and see how its working. You can click on the buttons to view the respective data.
Hide Unhide Rows Examples Case Study

Hide-UnHide Rows in Excel Worksheet using VBA – Download Example File

You can download the example file and see the code for each button.
ANALYSISTABS – Hide-Unhide Rows

Hiding Rows based on a Condition

Let us assume we have 500 rows/records in a worksheet, we can loop through each row and check for a criteria and hide the rows. Let us see the verity of example with different criteria:

Hide all rows with the text data /strings in Column A

The following example will hide all all rows with the text data /strings in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'To hide all the rows with the text data /strings  in Column A
If IsNumeric(Range("A" & i)) = False Then Rows(i).EntireRow.Hidden = True
Next

Hide all rows with the values as 0 in Column A

The following example will hide all all rows with the values as 0 in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'to hide all the rows with the values as 0 in Column A
If Range("A" & i) = 0 Then Rows(i).EntireRow.Hidden = True
Next

Hide all rows with the negative values in Column A

The following example will hide all all rows with the negative values in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'to hide all the rows with the negative values in Column A
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) < 0 Then Rows(i).EntireRow.Hidden = True
End If
Next

Hide all rows with the Positive values in Column A

The following example will hide all all rows with the Positive values in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'to hide all the rows with the Positive values in Column A
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) > 0 Then Rows(i).EntireRow.Hidden = True
End If
Next

Hide all rows with the Even Number in Column A

The following example will hide all all rows with the Even Number in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'To hide all the rows with the Even Number in Column A
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) Mod 2 = 0 Then Rows(i).EntireRow.Hidden = True
End If
Next

Hide all rows with the Odd Number in Column A

The following example will hide all all rows with the Odd Number in Column A.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'To hide all the rows with the Odd Number in Column A
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) Mod 2 = 1 Then Rows(i).EntireRow.Hidden = True
End If
Next

Hide all rows if the value in Column A is greater than 50

The following example will hide all all rows if the value in Column A is greater than 50.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'To hide all the rows if the value in Column A is greater than 50
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) > 50 Then Rows(i).EntireRow.Hidden = True
End If
Next

Hide all rows if the value in Column A is less than 100

The following example will hide all all rows if the value in Column A is less than 100.

LastRow = 500 'Let's say you have 500 rows in the data
For i = 1 To LastRow 'Now we loop through each row and check for required criteria
'To hide all the rows if the value in Column A is less than 100
If IsNumeric(Range("A" & i)) = True Then
If Range("A" & i) < 100 Then Rows(i).EntireRow.Hidden = True
End If
Next
Next

Hey! Join Our Community

Get Quick Responses & Experts' Answers in Minutes!
Get Notified - When Answered Your Question!