Hide UnHide Rows in Excel Worksheet using VBA

Home/Excel VBA/Hide UnHide Rows in Excel Worksheet using VBA

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:

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Business Presentations Templates Pack
PowerPoint Slides

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


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 22 to 25
Rows("5:8").EntireRow.Hidden = True
'To UnHide Rows 22 to 25
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
LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template
LIMITED TIME OFFER - Get it Now!
Business PowerPoint Presentations Templates Pack
 
 
Related Resource External VBA Reference
By |March 16th, 2013|Excel VBA|12 Comments

About the Author:

Excel VBA Developer having around 8 years of experience in using Excel and VBA for automating the daily tasks, reports generation and dashboards preparation. Valli is sharing to helps us automating daily tasks.

12 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. 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

  3. 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?

  4. 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

  5. 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?

  6. 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

  7. 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.

  8. 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?

Leave A Comment