Hiding Rows Based on a Cell Value

by Allen Wyatt
(last updated January 5, 2019)

19

Excel provides conditional formatting which allows you to change the color and other attributes of a cell based on the content of the cell. There is no way, unfortunately, to easily hide rows based on the value of a particular cell in a row. You can, however, achieve the same effect by using a macro to analyze the cell and adjust row height accordingly. The following macro will examine a particular cell in the first 100 rows of a worksheet, and then hide the row if the value in the cell is less than 5.

Sub HideRows()
    BeginRow = 1
    EndRow = 100
    ChkCol = 3

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value < 5 Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
End Sub

You can modify the macro so that it checks a different beginning row, ending row, and column by simply changing the first three variables set in the macro. You can also easily change the value that is checked for within the For ... Next loop.

You should note that this macro doesn't unhide any rows, it simply hides them. If you are checking the contents of a cell that can change, you may want to modify the macro a bit so that it will either hide or unhide a row, as necessary. The following variation will do the trick:

Sub HURows()
    BeginRow = 1
    EndRow = 100
    ChkCol = 3

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value < 5 Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1940) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Enlarging the Formula Bar

The Formula bar is used to display the formula that appears in a cell. You may want to modify how the Formula bar is ...

Discover More

Mysterious Blue Line between Paragraphs

Do you ever have mysterious lines show up between paragraphs either on your screen or on your printouts? It could be ...

Discover More

Searching for Items in an Automatically Numbered List

The Find and Replace capabilities of Word can be very powerful, but there are some things you cannot search for. One such ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Copying Formats to a New Worksheet

Do you want to copy formats from one worksheet to another? You can do so easily by using the Format Painter. It even ...

Discover More

Using Strikethrough Formatting

Need a line through the middle of your text? Use strikethrough formatting, which is easy to apply using the Format Cells ...

Discover More

Too Many Cell Formats

The error message "too many cell formats" can be difficult to fix. This tip describes ways you can attempt to get rid of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight minus 6?

2021-07-14 09:08:21

BMM

Hi all, this is a great little macro (especially for someone with limited experience. My use case is a bit different so I'm hoping I can get some help here. This macro works if all the rows you want to hide are one after the other. In my spreadsheet I have several groupings of rows that are not one after another. I need to identify several "ranges" or rows and then hide them based on a cell value for each row. Similar to this macro but for several groups of rows.

Additionally, I want to hide a few text "header" rows if the rows under that group are completely hidden. No need to show the header row if there's no content showing (because it was hidden with the macro described above). Can someone offer some suggestions? Thanks!


2020-12-25 11:30:03

Willy Vanhaelen

None of the variables in this tip's macros are declared which is bad practice.

Instead of defining the start row, end row and check column in the macro you can better simply select the rows in the check column and run this tiny macro:

Sub HURows()
Dim cell As Range
For Each cell In Selection
'change <> 4 to suit you need, i.e. < 5 as in this tip
cell.EntireRow.Hidden = cell <> 4
Next cell
End Sub


2020-09-08 10:41:18

Ronald

Nevermind! I figured it out.


2020-09-08 10:19:51

Ronald

What if the row start and end change if a user inserts or deletes a row above where the code is to begin. Can you name a row to make the code work? How would the code change?


2020-07-09 05:39:29

Alan Elston

Hi Vanessa
Most likely it can be done
In VBA, those sorts of things can usually be done in many different ways .
( But it’s a bit like asking if you can cut up a length of string. If you wanted a macro to do what you want, then you would need to be more specific about exactly what you want to do, which rows, which columns , how the macro should start, automatically on a cell entry or by you choosing to run it … etc….. etc… )
Alan ( Elston )


2020-07-08 06:16:31

vanessa

can you get excel to look at a row and only hide that row if all of the rows for particular columns are blank?


2020-06-20 08:11:18

Alan Elston

Hello Erica .. .. I have a solution and macros for you .. .. but it is a bit long to have pasted in Allen Wyatt’s comments section .. .. here is a link to a pair of spare forum post that I put it in .. .. https://excelfox.com/forum/showthread.php/2561-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)-Event-Coding?p=13748&viewfull=1#post13748 .. .. https://excelfox.com/forum/showthread.php/2561-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)-Event-Coding?p=13749&viewfull=1#post13749 .. … :: ( If you have problems getting at those links , then post your question again over at that forum ( excelfox.com ) , and someone will set you straight ) .. ... Alan Elston


2020-06-19 11:13:59

Erica

This is my script:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 And Target.Row = 5 Then
If Target.Value = "Yes" Then
Application.Sheets("FAR").Select
Application.Rows("14").Select
Application.Selection.EntireRow.Hidden = False
Application.Sheets("Additions").Select
Application.Rows("6").Select
Application.Selection.EntireRow.Hidden = False
ElseIf Target.Value = "No" Then
Application.Sheets("FAR").Select
Application.Rows("14").Select
Application.Selection.EntireRow.Hidden = True
Application.Sheets("Additions").Select
Application.Rows("6").Select
Application.Selection.EntireRow.Hidden = True
End If
End If
End Sub

How do I make this continue for the next 19 rows?

The idea is to:
hide row 14 in Tab FAR and Row 7 in Tab Additions if the value in row 6 Tab Additions is "NO" and leave unhidden if yes
hide row 15 in Tab FAR and Row 8 in Tab Additions if the value in row 7 Tab Additions is "NO" and leave unhidden if yes
hide row 16 in Tab FAR and Row 9 in Tab Additions if the value in row 8 Tab Additions is "NO" and leave unhidden if yes

and so fort 20 times

Please can you help me with the reloop of this script


2020-02-21 02:58:14

PS SP

Thank you very much for your formulation.


2019-11-29 05:54:52

Tufina

Hello there,
this is very helpful, I need to write a macro that hide the entire row if the cell in a designated column is red or beje. If the cell is not colored, I need the marco to take the value inside another column and copy it in that cell.

so basically, The column to analyze with the colors is Q, the macro needs to check if the cells in Q have these two specific color, if so, then the entire row needs to be hidden. If the cells in Q does not contain these two colors, I need the macro to copy the value in the same cell number but from row C into the Q cell.

I'm very new to VBA, would that be possible? I know the color index is 19 and 3.


2019-11-19 03:04:46

Alan Elston

Hi Karen
This is a good start point for Event type coding
https://www.youtube.com/watch?v=0EXdPcbsTZI


2019-11-18 07:07:47

Karen

Thanks Alan,
I did see that but didn't know if it applied, as I have not gotten very accustom to custom macro writing. I will research it. Thank you for letting me know this is the right track.


2019-11-18 06:03:50

Alan Elston

@Karen
Have a look at my answer to Ryanne a few comments down. That may give you some ideas.

Alan Elston


2019-11-17 14:17:57

Karen

Hi Alan,

I appreciate what you have provided here. I have created a customer form in excel that has a number sections. Some sections only apply to certain customers, based on options they choose from a validated list. With this bit of code, I can envision using an IF statement to populate a hidden cell with a number that would in turn allow an entire row hide (or unhide depending on the circumstance). My question is this, how do I make this code trigger on its own while the customer fills out unlocked fields on the excel "form" I have made?

Thanks again!!! Can't wait to get your advice!


2019-11-05 11:56:10

Jessica

best code I found online so far to hide row based on cell value in a particular column.
I modified it to hide the row based on the value in the 24th column that states NO DATA.

Sub HideRows()
BeginRow = 1
EndRow = 1600
ChkCol = 24

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "NO DATA" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
End Sub

Sub UnHideRows()
BeginRow = 1
EndRow = 1600
ChkCol = 24

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "NO DATA" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End Sub


2019-09-13 08:07:12

Alan Elston

Hello Ryanne

Rather than modifying the coding, it would probably be easier to use a simple “events” type coding which automatically kicks in when a range value is changed in a worksheet. Something of this form:.



Private Sub Worksheet_Change(ByVal Target As Range)

Target.EntireRow.Hidden = True

End Sub



This coding will need to be in a worksheet code module:

excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11486&viewfull=1#post11486



Alan Elston


2019-09-12 10:56:55

Ryanne

How would i modify the code to change automatically based on a change in cell value?


2019-05-23 03:24:00

Alan Elston

What do you have in your cells, Ryan?
( Allen Wyatt's coding des not error for me , even if i have text in some cells, as VBA will allow a < or > comparison of text with numbers )
Alan Elston


2019-05-22 14:15:57

Ryan

I've copy and pasted your second set of code directly into my sheet and get a type mismatch error.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.