How to Hide Rows Based on Cell Value in Excel (5 Methods)

We have a Sales dataset consisting of columns Order Date, Region, Product, and Quantity. We want to use any of the cell values in the column to hide rows.

Dataset-Hide Rows Based on Cell Value in Excel


Method 1 – Using the Filter Feature to Hide Rows Based on Cell Value

Steps:

  • Go to the Home tab.
  • Select Sort & Filter (from the Editing section).
  • Select Filter (from the Sort & Filter options).

Selecting Filter displays the Filter icon in each column header.

Filter-Hide Rows Based on Cell Value in Excel

  • Click on any filter icon in the column headers (i.e., Region).

Clicking on filter icon

  • The Filter command box appears. Uncheck any items (i.e., West) to hide their respective rows from the dataset.
  • Click on OK.

Filter command box

  • Excel hides the unticked entries (i.e., West) from the dataset and leaves all other entries on display as shown in the picture below.

Filter result


Method 2 – Using Formula and Filtering to Hide Rows in Excel Based on Cell Value

We’ll insert a custom string (i.e., Hide) in a helper column based on cell value to indicate whether we need to hide a row.

Steps:

  • Use the following formula in the helper cell F5.
=IF(E5<50,"Hide",E5)

The formula returns “Hide” if the respective cell in the E column has a value less than 50, or returns that value otherwise.

Formula -Hide Rows Based on Cell Value in Excel

  • Press Enter and drag the Fill Handle down to fill all cells in the helper column.

Fill handle

  • Follow the Steps of Method 1 to bring out the Filter command box.
  • Unselect the Hide value for column F.

Unselecting hide text

  • Here’s the result.

Formula result

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


Method 3 – Applying Excel Conditional Formatting to Hide Rows

Steps:

  • Go to the Home tab.
  • Select Conditional Formatting.
  • Select New Rule.

conditional formatting-Hide Rows Based on Cell Value in Excel

  • The New Formatting Rule window appears.
  • Select Use a formula to determine which cell to format as Select a Rule Type.
  • Insert the following formula under Edit the Rule Description.
 =$C5="West"
  • Click on Format.

New formatting Rule window

  • The Format Cells window appears.
  • Select the Number section.
  • Choose Custom (under the Category option).
  • Type 3 Semicolons (i.e., ;;;) under the Type section.
  • Click on OK.

Format cells window

  • Excel takes you back to the New Formatting Rule dialog box. Click OK.

New formatting Rule window

  • Here’s the result, with all rows that have the value “West” in Region reformatted to not display values.

Final result-Hide Rows Based on Cell Value in Excel


Method 4 – Hiding Rows Based on Cell Value Using VBA Macro

We changed the dataset so it starts from A1 and want to hide the rows depending on a column’s (i.e., Region) value equal to a cell value (i.e., East).

VBA Macro method

Steps:

  • Hit Alt + F11 to open the Microsoft Visual Basic window.
  • In the window, select Insert and choose Module.

vba -Hide Rows Based on Cell Value in Excel

  • Paste the following macro code in the Module and press F5 to run the macro.
Sub Hide_Rows_Based_On_Cell_Value()
StartRow = 2
EndRow = 15
ColNum = 2
For i = StartRow To EndRow
If Cells(i, ColNum).Value <> "East" Then
Cells(i, ColNum).EntireRow.Hidden = True
Else
Cells(i, ColNum).EntireRow.Hidden = False
End If
Next i
End Sub

macro code

The macro code assigns start (i.e., 2), end (i.e., 15) row and column (i.e., 2, Region Column) numbers. The column number declares in which column the macro matches the given value (i.e., East). Then the VBA IF function hides any rows except the East value existing in the rows of the given column (i.e., Region column).

  • Here’s the result.

vba macro code outcomes

Read More: VBA to Hide Rows in Excel


Method 5 – Using VBA Macro to Hide Rows Based on Cell Value in Real Time

We created a cell that stores the lookup value that we’ll use to hide rows that contain it.

real time vba -Hide Rows Based on Cell Value in Excel

Steps:

  • Open the Microsoft Visual Basic window (by pressing Alt + F11 altogether).
  • Double-click on the sheet name (i.e., Sheet3) under the VBAProject section.

Module insertion

  • Choose Worksheet from the first drop-down in the code window.

Selecting worksheet

  • The Private Sub appears.

Private Sub

  • Paste the following macro code in the code window.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
StartRow = 2
EndRow = 15
ColNum = 2
For i = StartRow To EndRow
If Cells(i, ColNum).Value = Range("A18").Value Then
Cells(i, ColNum).EntireRow.Hidden = True
Else
Cells(i, ColNum).EntireRow.Hidden = False
End If
Next i
End Sub

macro code-Hide Rows Based on Cell Value in Excel

The written macro code assigns start (i.e., 2), end (i.e., 15) row, and column (i.e., 2) numbers. Then it imposes a condition that it hides values equal to cell A18 in column 2. The VBA IF function creates a private macro code to hide rows in real time after entering any value in cell A18.

  • Hit F5 to run the macro then go back to the worksheet.
  • Type anything that exists in Column B and Press Enter.

VBA outcomes

  • The macro hides the rows that contain that text from the dataset. You can use any text or value of the assigned column to hide rows within a dataset.

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


Download the Excel Workbook


Related Articles


<< Go Back to Hide Rows | Rows in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Maruf Islam
Maruf Islam

MARUF ISLAM is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including ABACUS, AutoCAD, Rhinoceros, Maxsurf, and Hydromax. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he creates techy content... Read Full Bio

6 Comments
  1. Hi! I’m trying to apply some conditional formatting with a macro

    First: hide all rows (in the entire active sheet) if the value of the cell in Column G =”BID”, AND if the value of the cell in Column H <10000.00, AND if the value of the cell in Column I ="Orders, Web" OR ="cXML Orders"
    Second: hide all rows (in the entire active sheet) if the value of the cell in Column G ="MO", AND if the value of the cell in Column H <10000.00

    Then, as another separate macro to run after the above two are run:
    Apply RBG(253, 233, 217) color to any/all cells in Column G if the value of the cell in Column G is <10000.00

    If you could provide some direction on this, I would be so grateful!

    • ‘Thank you Dan for your comment. Let’s just your first row is 2 and last row is 7.

      ‘First condition: G = “BID”, H <= 10000.00, I = "Orders, Web" OR ="cXML Orders"

      Sub Hide_Condition1()
      Dim i As Integer
          For i = 2 To 5
      ' Cells (i,7) here 7 means G column and i will be 2, so it will be G2 cell and so on
              If Cells(i, 7).Value = "BID" And Cells(i, 8).Value <= 10000 And _
              (Cells(i, 9).Value = "Orders, Web" Or Cells(i, 9).Value = "cXML Orders") Then
                  Rows(i).EntireRow.Hidden = True
              Else
                  Rows(i).EntireRow.Hidden = False
              End If
          Next i
      End Sub

      ‘Second condition: G =”MO”, H <10000.00

      Sub Hide_Condition_2()
      Dim i As Integer
          For i = 2 To 5
              If Cells(i, 7).Value = "MO" And Cells(i, 8).Value <= 10000 Then
      
                  Rows(i).EntireRow.Hidden = True
              Else
                  Rows(i).EntireRow.Hidden = False
              End If
          Next i
      End Sub

      ‘Last Condition: G <10000.00, Apply Color

      Sub Hide_Condition_3()
      Dim i As Integer
          For i = 2 To 5
              If Cells(i, 7).Value < 10000 Then
              Cells(i, 7).Interior.Color = RGB(253, 233, 217)
              End If
          Next i
      End Sub

      ‘Hope this answer your query.

      • It was working perfectly.
        But my case.
        I need to apply this formula in sheet1 according to change the value in sheet2.
        It is functioning unless I protect sheet 1.

        How it possible to functioning even if the worksheet protected.

        • Greetings Ashif,

          You can manipulate (Hide or Unhide) the Rows of a Protected Worksheet by ticking the Use AutoFilter option under Allow all users of this worksheet to:

          [Go to the Review tab > Protect / Unprotect Sheet > Tick Use AutoFilter > Enter Password > Click OK]

          Hiding Rows in Protected Worksheet

          I hope this may work in your case. Let me know your thoughts in the comment section.

          Regards
          Maruf Islam (Exceldemy Team)

  2. Very helpful! At last! Saves me a lot of fiddling with the manual ‘hide’.

    • Hello John,

      You are most welcome. Glad to hear that it saved you from fiddling tasks.

      Regards
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo