This post will guide you how to hide rows based on cell value in Excel. How do I hide rows based on cell value with VBA Macro code in Excel 2013/2016.
Table of Contents
1. Hide Rows Based on Cell Value using Filter Feature
Assuming that you have a list of data in range A1:B6, and you want to hide row if the value of the column B is smaller than or greater than a specific value. You can use the Filter Function to filter numbers and hide the rows based on cell value. Here are the steps:
#1 select the range of cells that you want to hide rows based on cell values.
#2 go to DATA tab, click Filter button under Sort & Filter group. And the filter arrow will be inserted into the first cells in the selected columns.
#3 click Filter Arrow icon on the first cell on Sales column, and select Number Filters, then select Greater Than or Less Than menu from the popup submenu list. And the Custom AutoFilter dialog will open.
#4 type one number in the second text box under Sales section. At this example, we will type the number 300, it will filter out all rows that if the cell value is greater than number 300. Click Ok button.
#5 you should see that the rows should be hidden if the cell value is less than 300.
2. Hide Rows Based on Cell Value using VBA Macro
You can also use an Excel VBA Macro to achieve the same result of hiding rows based on cell value. Just do the following steps:
#1 open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.
#2 then the “Visual Basic Editor” window will appear.
#3 click “Insert” ->”Module” to create a new module.
#4 paste the below VBA code into the code window. Then clicking “Save” button.
Sub HideRowsBasedOnCellValue()
Dim rngSource As Range
Dim cellValue As String
Dim cell As Range
' Prompt user to select the source range
On Error Resume Next
Set rngSource = Application.InputBox("Select the source range:", Type:=8)
On Error GoTo 0
' Check if user canceled or did not select any range
If rngSource Is Nothing Then
MsgBox "No range selected. Operation cancelled.", vbExclamation
Exit Sub
End If
' Prompt user to enter the cell value to hide rows based on
cellValue = InputBox("Enter the cell value to hide rows based on:")
If cellValue = "" Then
MsgBox "No cell value entered. Operation cancelled.", vbExclamation
Exit Sub
End If
' Loop through each cell in the source range
For Each cell In rngSource
' Check if cell value matches the specified value
If cell.Value >= cellValue Then
' Hide the entire row if the cell value matches
cell.EntireRow.Hidden = True
End If
Next cell
MsgBox "Rows containing the specified cell value have been hidden.", vbInformation
End Sub
#5 back to the current worksheet, then run the above excel macro. Click Run button.
#6 Let’s see the result:
3. Video: Hide Rows Based on Cell Value
This Excel video tutorial, where we’ll explore two methods to hide rows based on a cell value. We’ll start by using the Filter feature, followed by leveraging VBA code.
Leave a Reply
You must be logged in to post a comment.