Hiding Rows Based on a Cell Value

by Allen Wyatt
(last updated January 5, 2019)

5

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

Extending a Paragraph into the Left Margin

Word allows you to format a paragraph so that it extends into the left margin of the document. This is done by setting a ...

Discover More

Adding Horizontal Lines at the Sides of a Word

Want to add a couple of horizontal lines at the sides of a word? It can be trickier than it sounds, but there are several ...

Discover More

Defining Default Printers on a Document Level

If you use multiple printers, you may wonder how to set each document in Word to remember which printer to use for that ...

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)

Controlling How Excel Interprets Percentages

When entering data in a worksheet, Excel tries to figure out how your entry can best be shown on the screen. When it ...

Discover More

Understanding Date and Time Formatting Codes

Want to apply a custom format to your dates and times? To do it effectively you need to understand the custom formatting ...

Discover More

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
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 8 + 0?

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.