Hiding Rows Based on a Cell Value

by Allen Wyatt
(last updated January 5, 2019)

11

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

Choosing an Insert Method for Pictures

The way that you choose to add pictures to your document can have an effect on the file size of those documents. It is ...

Discover More

Spell Checking Only Checking Grammar

Word has a built-in spelling and grammar checker that can help reduce errors in your prose. It may be a bit confusing if ...

Discover More

Adjusting Small Caps Text

If you use small caps text in a document, you know that there are several steps involved in properly formatting the text. ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Merge and Center Not Available

What are you to do if you are trying to format a worksheet, only to find out that one of the tools you need is not ...

Discover More

Handling Leading Zeros in CSV Files

When dealing with files containing comma-separated values, you want to make sure that what gets imported into Excel ...

Discover More

Superscripts in Custom Formats

When you create custom formats for your data, Excel provides quite a few ways you can make that data look just as you ...

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 1 + 9?

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.