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 – An Example
- Hide-UnHide Rows in Excel Worksheet using VBA – Case study
- Hide-UnHide Rows in Excel Worksheet using VBA – Download Example Files
- Hiding Rows with Criteria in Excel Worksheet using VBA – Example File
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:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- 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 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 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
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.
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!
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
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!
Awesome!! Works like a charm,
Thanks again!
Welcome! I’m glad it worked. Thanks-PNRao
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?
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
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?
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
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.
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?
How I can to hide Rows (A4:A6), and unhide Rows (A1:A3) in same time?
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!!!
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
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
Thanks you, Updated!