Table of Contents
VBA, or Visual Basic for Applications, is a programming language that can be used to automate tasks in Excel. One useful function of VBA is the ability to hide rows in an Excel spreadsheet based on specific criteria. This can be achieved by writing a VBA code that checks for the specified criteria and then uses the Hidden” property to hide the rows that meet the criteria. This allows for a more efficient and organized way to manage data in Excel, as it eliminates the need for manually hiding rows one by one. Additionally, using VBA to hide rows based on specific criteria allows for greater customization and flexibility, as the criteria can be easily modified to suit different needs. This feature is particularly useful for managing large datasets and creating dynamic reports in Excel.
VBA: Hide Rows Based on Criteria
You can use the following syntax in VBA to hide rows based on a cell value:
Sub HideRows()
Dim i As Integer
For i = 2 To 10
If Cells(i, 1).Value = "Mavs" Then
Cells(i, 1).EntireRow.Hidden = True
Else
Cells(i, 1).EntireRow.Hidden = False
End IfNext i
End Sub
This particular macro hides all rows in the range from rows 2 to 10 that have a cell value equal to “Mavs” in the first column.
You can also use the following macro to unhide all rows:
Sub UnhideRows()
Rows.EntireRow.Hidden = False
End SubThe following example shows how to use this syntax in practice.
Example: Use VBA to Hide Rows Based on Criteria
Suppose we have the following dataset that contains information about various basketball players:

Suppose that we would like to hide each row that contains “Mavs” in the team column.
We can create the following macro to do so:
Sub HideRows()
Dim i As Integer
For i = 2 To 10
If Cells(i, 1).Value = "Mavs" Then
Cells(i, 1).EntireRow.Hidden = True
Else
Cells(i, 1).EntireRow.Hidden = False
End IfNext i
End SubWhen we run this macro, we receive the following output:

Notice that each row that contained “Mavs” in the team column has now been hidden.
If we’d like, we can create the following macro to unhide all rows:
Sub UnhideRows()
Rows.EntireRow.Hidden = False
End Sub
Notice that all of the rows are now shown again.
Note that in our macro we specified For i = 2 To 10 to hide rows in the range from 2 to 10 based on criteria.
Feel free to change these starting and ending values to hide rows in a different row range.
Cite this article
stats writer (2024). How can I use VBA to hide rows in Excel based on specific criteria?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-hide-rows-in-excel-based-on-specific-criteria/
stats writer. "How can I use VBA to hide rows in Excel based on specific criteria?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-hide-rows-in-excel-based-on-specific-criteria/.
stats writer. "How can I use VBA to hide rows in Excel based on specific criteria?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-hide-rows-in-excel-based-on-specific-criteria/.
stats writer (2024) 'How can I use VBA to hide rows in Excel based on specific criteria?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-hide-rows-in-excel-based-on-specific-criteria/.
[1] stats writer, "How can I use VBA to hide rows in Excel based on specific criteria?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use VBA to hide rows in Excel based on specific criteria?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
