Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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.
by Allen Wyatt
(last updated November 21, 2015)
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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11746) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Hiding Rows Based on Two Values.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
It is not unusual to need to select two random items from a list. There are a couple of ways you can approach the task; ...Discover More
If you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...Discover More
Sometimes it can be confusing to figure out the source of an error that is displayed in your worksheet. Excel provides a ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.