Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Hiding Rows Based on Two Values.

Hiding Rows Based on Two Values

Written by Allen Wyatt (last updated February 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


Mike, as an accountant, has a need to hide rows in a worksheet based on the values in two cells in the row. His data tables have three columns; if a row contains a zero in columns two and three, then the row should be hidden. If either column two or three is blank or contains some other value, then the row should not be hidden.

There are a couple of ways you can approach this problem. The first is to use Excel's filtering capabilities. Just create another column that contains a formula such as this:

=AND(B2=0,C2=0)

The value returned by the formula will be True only if both the second (B) and third (C) columns contain a zero value. Copy the formula to the other appropriate cells in the column, and you can then apply a filter based on that column. When you display only those rows containing a False in the column, then you have effectively hidden the rows in which there is a zero value in columns two and three.

You can also use a macro to check out the rows for you. The following macro steps through each row in the worksheet, beginning with row 1. As long as there is something in column A, then the macro checks to make sure that there is a zero value in columns B and C. If there is, then the .Hidden property for the row is set.

Sub Hide()
    Dim Criteria as Boolean
    Dim i As Integer

    i = 1
    Do Until Trim(Cells(i, 1).Value) = "" 
        Criteria = True
        Criteria = Criteria And (Cells(i, 2).Value = 0) _
          And Cells(i, 2).Value <> ""
        Criteria = Criteria And (Cells(i, 3).Value = 0) _
          And Cells(i, 3).Value <> "" 
        If Criteria Then Rows(i).EntireRow.Hidden = True
        i = i + 1 
    Loop 
End Sub

The macro runs until such time as it encounters a row where there is nothing in column A. This means that you need to make sure there is actually something in the rows before your data table. If your data table starts in row 4 of the worksheet, and cells A1 through A3 have nothing in them, then the macro will never run satisfactorily. You can, of course, adjust the macro in this situation so that it starts checking in row 4; simply change the initial assignment of the i variable to 4 instead of 1.

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 (11746) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Hiding Rows Based on Two Values.

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

Saving in Document Format from a Macro

Saving a document in a different format is easy if you are manually using the Save As command. Saving a document in an ...

Discover More

Getting the Proper Type of Ellipses

Type three periods in a row, and the AutoCorrect feature in Word kicks in to exchange that sequence for a special ...

Discover More

Generating Unique, Sequential Names

Do you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Summing Absolute Values

You can easily sum a series of values in Excel, but it is not so easy to sum the absolute values of each value in a ...

Discover More

Ignoring N/A Values in a Sum

You can use some of Excel's worksheet functions across a range or worksheets, but not all of them. One that has problems ...

Discover More

Finding the Address of the Lowest Value in a Range

Uncovering the lowest value in a range is relatively easy; you can just use the MIN worksheet function. Discovering the ...

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}] (all 7 characters, in the sequence shown) 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 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.