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 5 to 8
Rows("5:8").EntireRow.Hidden = True

'To UnHide Rows 5 to 8
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
120+ Professional Project Management Templates!
Save Up to 85% LIMITED TIME OFFER

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Browse All Templates
Excel VBA Project Management Templates

All-in-One Pack
120+ Project Management
Premium Templates
View Details

Essential Pack
50+ Project Management
Premium Templates
View Details
50+ Excel
Project Management
Templates Pack
View Details
50+ PowerPoint
Project Management
Templates Pack
View Details
25+ MS Word
Project Management
Templates Pack
View Details
Ultimate Project Management Template
View Details
Ultimate Resource Management Template
View Details
Project Portfolio Management Templates
View Details
By Last Updated: June 17, 2022Categories: Excel VBATags:

Share This Story, Choose Your Platform!

17 Comments

  1. LS July 24, 2015 at 10:23 AM - Reply

    In case I want to hide a row with condition…… for eg. I have a data base with some columns having text and some having numbers. If value in one of the columns becomes 0 or a negative number or a text then that row should get hidden.

    could you please automate such a task in excel ?

    thanks.

  2. PNRao July 25, 2015 at 3:04 AM - Reply

    Hi Lata,

    I have updated the post with number of examples with criteria.
    Please check now:Hiding Rows with Criteria in Excel Worksheet using VBA – Example File
    Thanks-PNRao!

  3. Ben August 7, 2015 at 8:00 PM - Reply

    I tried to apply the same principle to columns with the conditions in row 1, but it can’t run.
    Maybe someone knows what I need to fix in the script below :
    Thanks!

    LastColumn = “EZ” ‘Let’s say I have columns until “EZ” in the report
    For i = “A” To LastColumn ‘I want to loop through each column and check for required criteria

    ‘to hide all the columns with the values as 0 in Row 1
    If Range(1 & i) = 0 Then Column(i).EntireColumn.Hidden = True

    Next

  4. PNRao August 8, 2015 at 3:25 PM - Reply

    Hi Ben,

    For loop takes numeric values, replacing your column names with column numbers will work for you, check the below examples

    http://analysistabs.com/excel-vba/hide-unhide-columns-worksheet/#bm4

    Thanks-PNRao!

  5. Ben August 11, 2015 at 7:21 PM - Reply

    Awesome!! Works like a charm,

    Thanks again!

  6. PNRao August 11, 2015 at 11:51 PM - Reply

    Welcome! I’m glad it worked. Thanks-PNRao

  7. patrick December 7, 2015 at 5:40 PM - Reply

    My desire is to specific cells. I have checkboxes and command buttons sprinkled throughout the worksheet. What I like to do is to hide the cells containing these form types. Maybe I have to redesign my worksheet to have all these buttons and boxes either all in a row or a column and hide the row/column. Have not had much luck with this. Can you help please?

  8. amina1987 March 9, 2016 at 4:16 PM - Reply

    good morning.
    thank you for this very usefull topic.

    for my work (for 2400 workbooks) i need to hide specific rows without openning the workbooks. is that possible? please help

  9. Avishek Bose April 16, 2016 at 4:02 PM - Reply

    Hey Valli,

    This is my first attempt to actually write a Macro, and looks like I am stuck (Terribly Stuck)

    Here is what I am trying to achieve:
    I have a “Input” sheet which is linked (Via Vlookups in C:C) to 8-10 other worksheets (containing rates of materials). On a daily basis I have to insert the quantities (in Column D:D) next to the rates in the “Input” Sheet to multiply it with the corresponding rates to arrive at the total amount (in Column E:E) eventually.
    The issue is I have to hide the materials (Entire Rows) I do not use in the Input sheet as well as the other reference sheets. Is there any way a macro can be written to help me?

  10. Guy May 9, 2016 at 4:27 PM - Reply

    HI PNRao,

    Thanks for the great poste it really helped me, how would i loop this macro round to unhide the hidden rows when the box is unchecked?

    Thanks

  11. SANDIP DAVE January 3, 2017 at 8:01 PM - Reply

    I have a worksheet with live data of stock value, high, low in Column A3:A103, B3:B103, C3:C103 respectively and other formula in other columns and that updates every few seconds.
    I would like to see only rows where below conditions is fulfilled and hide all other rows to filter out stocks.
    Condition 1: =AND(A3=B3,A3>=L3,A3<=K3,A30)
    Condition 2 =AND(A3=C3,A3>=S3,A3<=R3,A30)
    Above condition is for example of Raw No.3 and which I am using for conditional formatting.
    Kindly help to provide VBA codes for this.

  12. Skup June 26, 2017 at 11:52 AM - Reply

    Greetings,

    How would someone show/hide rows in a 2nd range based upon the visibility of rows in a filtered range?

    For example, if rows 5 through 20 are autofiltered by some criteria such that at a given state only rows 6, 8. 12. and 16 through 18 are visible, how could someone then show the matching rows in the range of rows from 105 through 120, so that along with rows 6, 8. 12. and 16 through 18 being visible by filtering; rows 106, 108, 112, 116, 117, and 118 are visible, while rows 105, 107, 109, 110, 11, 113, 114, 115, 119, and 120 are hidden?

    How can this be triggered by the setting and resetting of the autofilter on the filtered range?

  13. Orlando March 21, 2019 at 8:18 AM - Reply

    How I can to hide Rows (A4:A6), and unhide Rows (A1:A3) in same time?

  14. Carlos February 4, 2020 at 8:42 PM - Reply

    Hi Everyone,

    I need help to write a VB macro which allows me to use a Hide Rows/Unhide Rows button(s) using a zero value or blank range in Column D. I’ve tried taking various examples from here and other sites to try to help but either I’m doing something wrong (most likely) or the code wasn’t correct for my requirements in the first place!

    Can anyone help please, I’m so lost!!!

  15. Jasper April 11, 2020 at 12:42 AM - Reply

    In your first example you say you’re hiding rows 5-8
    IN the code you say Hide Rows 22-25
    with the code set to 5:8

    Just got me confused

  16. Eugene Van Loggerenberg May 20, 2020 at 4:21 PM - Reply

    Good day

    I am just starting out and have zero VBA experience.
    I have been able to create a button and link it to VBA and that part works.
    Using the “Hide all rows with the values as 0 in Column A” I am having problems getting it to work.

    Assistance would be much appreciated.

    Regards Eugene

  17. PNRao September 6, 2020 at 8:55 PM - Reply

    Thanks you, Updated!

Leave A Comment