VBA to Hide Rows Based on Criteria in Excel (15 Useful Examples)

Sometimes we need to hide some specific rows based on various criteria from our large dataset in Excel for better readability. Implementing VBA macro is the most effective, quickest and safest method to run any operation in Excel. In this article, we will show you how to hide rows based on criteria in Excel with the VBA macro.


How to Hide Rows Based on Criteria with VBA in Excel: 15 Suitable Methods

In this section, we will discuss 15 different methods with VBA to hide rows based on different criteria in Excel.

Dataset for VBA to Hide Rows Based on Criteria in Excel

Above is the example dataset that we will be using throughout this whole article.


Criteria 1: Embed VBA to Hide a Single Row in Excel

If you want to hide a single row with VBA code, then follow the steps discussed below. In our case, we will hide row number 5 (Last Name) from our dataset.

Steps:

  • In the beginning, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • Next, in the pop-up code window, from the menu bar, click Insert -> Module.

  • Then, copy the following code and paste it into the code window.
Sub HideSingleRow()
    Worksheets("Single").Range("5:5").EntireRow.Hidden = True
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Single Row Criteria in Excel

Here,

  • Worksheets(“Single”) = Set the worksheet name.
  • Range(“5:5”) = Pass row number 5 inside the Range method.
  • Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Run icon in the sub-menu bar to run the macro.

After the successful code execution, look at the image below to find out the result.

Finally, row number 5 is hidden after successfully executing the VBA code.

VBA Code Explanation

Sub HideSingleRow()

First, provide a name for the sub-procedure of the macro.

Worksheets("Single").Range("5:5").EntireRow.Hidden = True

Then, hide the entire 5th row from the worksheet named “Single“.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 2: Insert VBA to Hide Contiguous Rows in Excel

You have learned how to hide a single row with VBA. But suppose, you want to hide a range of rows that are contiguous. You can do that too with VBA in Excel. We will hide rows number 5 to 7 from our dataset shown above.

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideContiguousRows()
    Worksheets("Contiguous").Range("5:7").EntireRow.Hidden = True
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Contiguous Row Criteria in Excel

Here,

  • Worksheets(“Contiguous”) = Set the worksheet name.
  • Range(“5:7”) = Pass row number 5 to 7 inside the Rangemethod.
  • After that, Run the macro as we showed you in the above section. The result is shown in the image below.

You can see from the above image that after successful code execution, rows 5 to 7 are hidden now.

VBA Code Explanation

Sub HideContiguousRows()

First, provide a name for the sub-procedure of the macro.

Worksheets("Contiguous").Range("5:7").EntireRow.Hidden = True

Then, hide entire rows from the 5th row to the 7th row from the worksheet named “Contiguous“.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 3: Embed VBA to Hide Non-Contiguous Rows in Excel

This time you will learn how to hide rows in Excel that are non-contiguous with VBA. With our dataset, the code to hide rows 5, 6, 8 and 9 are given below.

Steps:

  • As previously shown, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideNonContiguousRows()
    Worksheets("Non-Contiguous").Range("5:6, 8:9").EntireRow.Hidden = True
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Non Contiguous Row Criteria in Excel

Here,

  • Worksheets(“Non-Contiguous”) = Set the worksheet name.
  • Range(“5:6, 8:9”) = Pass row number 5 to 6 and 8 to 9 inside the Range method.
  • After that, Run this piece of code and the result is shown in the following image.

As a result, rows 5 to 6 and 8 to 9 are now hidden after successful code execution.

VBA Code Explanation

Sub HideNonContiguousRows()

First, provide a name for the sub-procedure of the macro.

Worksheets("Non-Contiguous").Range("5:6, 8:9").EntireRow.Hidden = True

Then, hide the entire 5th, 6th, 8th and 9th rows from the worksheet named “Non-Contiguous“.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 4: Insert VBA to Hide All Rows Containing Texts in Excel

If you want to hide all rows that contain text values, then follow the steps provided below.

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Secondly, copy the following code and paste it into the code window.
Sub HideAllRowsContainsText()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 1 To LastRow 'Loop through each row and check for required condition
    'To hide all the rows with the text data
    If IsNumeric(Range("C" & i)) = False Then Rows(i).EntireRow.Hidden = True
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on All Texts Criteria in Excel

Here,

  • IsNumeric(Range(“C” & i)) = The data in our dataset starts from column C, so we passed C inside the Range method.
  • After that, Run this code.

Lastly, you will notice all the rows that contained text values are now hidden from the dataset.

VBA Code Explanation

Sub HideAllRowsContainsText()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 1 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition.

If IsNumeric(Range("C" & i)) = False Then Rows(i).EntireRow.Hidden = True

If there are non-numeric values in Column C then hide all the rows from that column that hold the numeric values. Our data starts from Column C, that’s why we pass Column C as the parameter. You must modify this line according to your dataset.

Next

To continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 5: Embed Macro to Cloak All Rows Containing Numbers in Excel

If you want to hide all rows that contain numeric values with VBA code, then follow the steps provided below.

Steps:

  • First, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideAllRowsContainsNumbers()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
    'To hide all the rows with the numeric data
    If IsNumeric(Range("C" & i)) = True Then Rows(i).EntireRow.Hidden = True
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on All Numbers Criteria in Excel

Here,

  • IsNumeric(Range(“C” & i)) = The data in our dataset starts from column C, so we passed C inside the Range method.
  • Lastly, Run this code and notice the image below.

Consequently, all the rows that contained numeric values before are now hidden.

VBA Code Explanation

Sub HideAllRowsContainsNumbers()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("C" & i)) = True Then Rows(i).EntireRow.Hidden = True

If there are numeric values in Column C then hide all the rows from that column that hold the numeric values. Our data starts from Column C, that’s why we pass Column C as the parameter. You must modify this line according to your dataset.

Next

To continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 6: Implement Macro to Hide Rows Containing Zero (0) in Excel

Suppose you want to hide only the rows from a specific column that are holding 0 (zero). Look at the following dataset where the E column holds 0 in row 7 and 82 in row 10. We will learn how to hide only the row that is holding 0 (row 7) with VBA in Excel.

Dataset for VBA to Hide Rows Based on Zeros Criteria in Excel

Steps:

  • First, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Second, copy the following code and paste it into the code window.
Sub HideRowContainsZero()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the row containing 0 in E column
        If Range("E" & i) = 0 Then Rows(i).EntireRow.Hidden = True
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Zeros Criteria in Excel

  • Third, Run this code, and let’s see what happened as the result.

As a result, the row (row 7) that contains 0 in column E is now hidden whereas row 10 which carries 82 is unhidden.

VBA Code Explanation

Sub HideRowContainsZero()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If Range("E" & i) = 0 Then Rows(i).EntireRow.Hidden = True

If Column E holds 0, then hide all the rows from that column that hold the value. You can modify this line by passing the column address according to your dataset.

Next

To continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.

Read More: How to Hide Rows with Zero Values in Excel Using Macro


Criteria 7: Implement VBA Macro to Hide Rows Holding Negative Values

Just as you can hide rows that contain zero from the same column, you can hide rows that hold negative values as well. With the dataset shown below where column E contains both negative and positive values, we will see the VBA code that hides only the negative ones.

Dataset for VBA to Hide Rows Based on Negative Value Criteria in Excel

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideRowContainsNegative()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing negative values in E column
        If IsNumeric(Range("E" & i)) = True Then
            If Range("E" & i) < 0 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Negative Value Criteria in Excel

  • Next, Run this piece of code.

As you can see from the image above, row 7 which contains the negative value (-10) in column E is now hidden whereas row 10 which carries 82 is unhidden.

VBA Code Explanation

Sub HideRowContainsNegative()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("E" & i)) = True Then
    If Range("E" & i) < 0 Then Rows(i).EntireRow.Hidden = True
End If

If Column E holds numeric values that are less than 0, that means they are negative values, then hide the entire rows that carry negative values from that column.

Next

Continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 8: Embed VBA to Conceal Rows Containing Positive Values

This time with the dataset given below where column E contains both zero and positive values, we will see the VBA code that hides only the positive ones.

Dataset for VBA to Hide Rows Based on Positive Value Criteria in Excel

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Secondly, copy the following code and paste it into the code window.
Sub HideRowContainsPositive()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing positive values in E column
        If IsNumeric(Range("E" & i)) = True Then
            If Range("E" & i) > 0 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

 VBA to Hide Rows Based on Positive Value Criteria in Excel

  • After that, Run this code, and the result is shown in the following image.

Finally, row 7 which contains the positive value (55) in column E is now hidden whereas row 10 which carries 0 is unhidden.

VBA Code Explanation

Sub HideRowContainsPositive()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("E" & i)) = True Then
    If Range("E" & i) > 0 Then Rows(i).EntireRow.Hidden = True
End If

If Column E holds numeric values that are greater than 0, that means they are positive values, then hide the entire rows that carry positive values from that column.

Next

Continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 9: Macro to Hide Rows That Contain Odd Numbers in Excel

You can hide rows that have odd numbers as their value. In the example that we showed at the beginning of this article, column E holds both odd and even numbers in rows 7 and 10. We will learn the code for hiding the row that holds odd numbers only.

Steps:

  • In the beginning, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideRowContainsOdd()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing odd values in E column
        If IsNumeric(Range("E" & i)) = True Then
            If Range("E" & i) Mod 2 = 1 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Odd Number Criteria in Excel

  • Later, Run this piece of code.

Finally, only row 7 which contains the odd number (55) in column E is now hidden whereas row 10 which carries the even number (82) is unhidden.

VBA Code Explanation

Sub HideRowContainsOdd()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("E" & i)) = True Then
    If Range("E" & i) Mod 2 = 1 Then Rows(i).EntireRow.Hidden = True
End If

If Column E holds numeric values that produce 1 as the remainder when divided by 2, that means which are odd values, then hide the entire rows that carry odd numbers from that column.

Next

Continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 10: VBA to Hide Rows Containing Even Numbers in Excel

Similarly, you can hide rows that have even numbers as their value. In the example that we showed at the beginning of this article, column F holds both odd and even numbers in rows 7 and 10. We will learn the code for hiding the row that holds even numbers only.

Steps:

  • First, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Second, copy the following code and paste it into the code window.
Sub HideRowContainsEven()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing even values in F column
        If IsNumeric(Range("F" & i)) = True Then
            If Range("F" & i) Mod 2 = 0 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Even Number Criteria in Excel

  • Third, Run this code and see the result in the following image.

Lastly, only row 10 which contains the even number (75) in column F is now hidden whereas row 7 which carries the even number (100) is unhidden.

VBA Code Explanation

Sub HideRowContainsEven()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("F" & i)) = True Then
    If Range("F" & i) Mod 2 = 0 Then Rows(i).EntireRow.Hidden = True
End If

If Column F holds numeric values which produce 0 as the remainder when divided by 2, that means which are even values, then hide the entire rows that carry even numbers from that column.

Next

To continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.

 


Criteria 11: Insert VBA to Hide Rows That Are Greater Than a Specific Condition in Excel

You can hide rows that are greater than a specific value with VBA Excel. Suppose you want to hide the rows from column E where the value is greater than 80. Here’s how to do that:

Steps:

  • First, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideRowContainsGreater()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing values greater than 80 in E column
        If IsNumeric(Range("E" & i)) = True Then
            If Range("E" & i) > 80 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Greater Than Criteria in Excel

  • Later, Run this code.

As a result, only row 10 which contains 82 (which is greater than 80) in column E is now hidden whereas row 7 which carries 55 is unhidden.

VBA Code Explanation

Sub HideRowContainsGreater()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("E" & i)) = True Then
    If Range("E" & i) > 80 Then Rows(i).EntireRow.Hidden = True
End If

If Column E holds numeric values that are greater than 80, then hide the entire rows that carry those numbers from that column.

Next

Continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 12: Embed VBA to Hide Rows That Are Less Than a Specific Condition in Excel

You can also hide rows that are less than a specific value with VBA Excel. Suppose you want to hide the rows from column E where the value is less than 80. Here’s how to do that:

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Secondly, copy the following code and paste it into the code window.
Sub HideRowContainsLess()
    LastRow = 1000 'Let's assume there are 1000 rows in the dataset
    For i = 4 To LastRow 'Loop through each row and check for required condition
    'We set i = 4 because our data starts from row 4
        'To hide the rows containing values less than 80 in E column
        If IsNumeric(Range("E" & i)) = True Then
            If Range("E" & i) < 80 Then Rows(i).EntireRow.Hidden = True
        End If
    Next
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Less Than Criteria in Excel

  • Thirdly, Run this piece of code. The result is shown in the image below.

As a result of the successful code execution, only row 7 which contains 55 (which is less than 80) in column E is now hidden whereas row 10 which carries 82 is unhidden.

VBA Code Explanation

Sub HideRowContainsLess()

First, provide a name for the sub-procedure of the macro.

LastRow = 1000

Then, let’s assume there are 1000 rows in the dataset.

For i = 4 To LastRow

Next, start looping through each row from the first row to the last row and check for the required condition. We set i = 4 because our data starts from row 4. You must modify this line according to your dataset.

If IsNumeric(Range("E" & i)) = True Then
    If Range("E" & i) < 80 Then Rows(i).EntireRow.Hidden = True
End If

If Column E holds numeric values that are less than 80, then hide the entire rows that carry those numbers from that column.

Next

To continue the FOR Loop till it reaches all the rows.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 13: Macro to Hide Rows Based on Cell Text Value in Excel

Suppose you want to hide a row that holds a specific text value. In our case, we will give you an example with the row that contains the word “Chemistry”, row 6 and provide you with the code on how to hide that row.

Steps:

  • In the beginning, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub HideRowCellTextValue()
    StartRow = 4
    LastRow = 10
    iCol = 4
    For i = StartRow To LastRow
        If Cells(i, iCol).Value <> "Chemistry" Then
        Cells(i, iCol).EntireRow.Hidden = False
        Else
        Cells(i, iCol).EntireRow.Hidden = True
        End If
    Next i
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Text Value Criteria in Excel

Here,

  • StartRow = 4 -> First row of the dataset.
  • LastRow = 10 -> Last row of the dataset.
  • iCol = 4 -> The column address that holds the text value.
  • Next, Run this code, and the result is shown in the image below.

Subsequently, row number 6 which consists of the word “Chemistry” is now hidden from our dataset.

VBA Code Explanation

Sub HideRowCellTextValue()

First, provide a name for the sub-procedure of the macro.

StartRow = 4

Then, declare the first row of the dataset. We set StartRow = 4 because our data starts from row 4. You must modify this line according to your dataset.

LastRow = 10

Next, declare the last row of the dataset. We set LastRow = 10 because our data ends in row 10. You must modify this line according to your dataset.

iCol = 4

After that, declare the column address that holds the text value. We will hide rows based on the value residing in Column D, so we set iCol = 4. You must modify this line according to your dataset.

For i = StartRow To LastRow
    If Cells(i, iCol).Value <> "Chemistry" Then
        Cells(i, iCol).EntireRow.Hidden = False
    Else
        Cells(i, iCol).EntireRow.Hidden = True
    End If
Next i

Now, starts looping from the declared first row (4) to the declared last row (10). If the iteration variable i finds the word “Chemistry” in any row of the declared column (D), then hides the entire rows. It continues iterating until it reaches all the rows from that column.

End Sub

Finally, end the sub-procedure of the macro.


Criteria 14: VBA Macro to Hide Rows Based on Cell Numeric Value in Excel

Previously you have seen how to hide a row based on cell text value, this time you will learn how to do that when the value is numeric. In our case, we will give you an example with the row that contains the numeric value “87”, row 7 and provide you with the code on how to hide that row.

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Secondly, copy the following code and paste it into the code window.
Sub HideRowCellNumValue()
    StartRow = 4
    LastRow = 10
    iCol = 4
    For i = StartRow To LastRow
        If Cells(i, iCol).Value <> "87" Then
        Cells(i, iCol).EntireRow.Hidden = False
        Else
        Cells(i, iCol).EntireRow.Hidden = True
        End If
    Next i
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Numeric Value Criteria in Excel

Here,

  • StartRow = 4 -> First row of the dataset.
  • LastRow = 10 -> Last row of the dataset.
  • iCol = 4 -> The column address that holds the text value.
  • After that, Run this piece of code.

Finally, row number 7 which consists of the numeric value “87” is now hidden.

VBA Code Explanation

Sub HideRowCellNumValue()

First, provide a name for the sub-procedure of the macro.

StartRow = 4

Then, declare the first row of the dataset. We set StartRow = 4 because our data starts from row 4. You must modify this line according to your dataset.

LastRow = 10

Next, declare the last row of the dataset. We set LastRow = 10 because our data ends in row 10. You must modify this line according to your dataset.

iCol = 4

After that, declare the column address that holds the textvalue. We will hide rows based on the value residing in Column D, so we set iCol = 4. You must modify this line according to your dataset.

For i = StartRow To LastRow
    If Cells(i, iCol).Value <> "87" Then
        Cells(i, iCol).EntireRow.Hidden = False
    Else
        Cells(i, iCol).EntireRow.Hidden = True
    End If
Next i

Now, starts looping from the declared first row (4) to the declared last row (10). If the iteration variable i finds the number “87” in any row of the declared column (D), then hides the entire rows. It continues iterating until it reaches all the rows from that column.

End Sub

Finally, end the sub-procedure of the macro.

Read More: VBA to Hide Rows Based on Cell Value in Excel 


Criteria 15: Implement Macro to Hide Rows Based on Inserted Condition in Worksheet

In this section, you will learn how to hide rows based on the criteria that you provided in the working sheet in Excel. Consider the following image. We will hide the row based on the value inserted in Cell E12.

Dataset for VBA to Hide Rows Based on Inserted Criteria in Excel

Steps to execute that are given below.

Steps:

  • First, right-click on the worksheet of interest. A list of options will appear.
  • From the appeared option list, select View Code.

  • You will be redirected to the code window of the specific worksheet. Copy the following code and paste it into the code window.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    StartRow = 4
    LastRow = 10
    iCol = 5
    For i = StartRow To lastRow
        If Cells(i, iCol).Value = Range("E12").Value Then
            Cells(i, iCol).EntireRow.Hidden = True
    Else
        Cells(i, iCol).EntireRow.Hidden = False
        End If
    Next i
End Sub

Your code is now ready to run.

VBA to Hide Rows Based on Inserted Criteria in Excel

Here,

  • StartRow = 4 -> First row of the dataset.
  • LastRow = 10 -> Last row of the dataset.
  • iCol = 5 -> The column address that holds the value; based on which we will hide the row.
  • Don’t run this code, save it.
  • Now, go back to the worksheet of interest. Insert any value from Column E in Cell E12. You will see that the whole row that contains the value will be hidden from that worksheet.

As you can see from the gif above, we inserted “Biology” in Cell E12 and as a result, the whole row 6 is now hidden.

VBA Code Explanation

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

First, initiating an event under the specific worksheet where Target is passed as an argument with Range type. When the Range changes, the event occurs.

StartRow = 4

Then, declare the first row of the dataset. We set StartRow = 4 because our data starts from row 4. You must modify this line according to your dataset.

LastRow = 10

Next, declare the last row of the dataset. We set LastRow = 10 because our data ends in row 10. You must modify this line according to your dataset.

iCol = 5

After that, declare the column address that holds the text value. We will hide rows based on the value residing in Column E, so we set iCol = 5. You must modify this line according to your dataset.

For i = StartRow To lastRow
    If Cells(i, iCol).Value = Range("E12").Value Then
        Cells(i, iCol).EntireRow.Hidden = True
    Else
        Cells(i, iCol).EntireRow.Hidden = False
    End If
Next i

Now, starts looping from the declared first row (4) to the declared last row (10). If the iteration variable i finds any value in Cell E12 and if the value matches with any value in the declared column (E), then hides the entire rows. It continues iterating until it reaches all the rows from that column.

End Sub

Finally, end the sub-procedure of the macro.


Download Practice Workbook


Conclusion

This article showed you 15 different methods on how to hide rows based on criteria in Excel with VBA. I hope this article has been very beneficial to you. Feel free to ask if you have any questions regarding the topic.


Related Articles:

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo