
Tips.Net > ExcelTips Home > Formatting > Row Formatting > Hiding Rows Based on Two Values
Summary: A common task in accounting (and other areas) is to suppress the display of tables in a row based on whether the values in two cells of a row meet a particular criteria. This tip explains some easy ways you can check those cells and modify the visibility of the row accordingly. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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, and 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 AutoFilter. 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 use Data | Filter | AutoFilter to filter the data 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 data table, 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, 2).Value <> ""
If Criteria Then Rows(i).EntireRow.Hidden = True
i = i + 1
Loop
End Sub
Tip #2416 applies to Microsoft Excel versions: 97 2000 2002 2003
Tame Your Data! ExcelTips: Filters and Filtering provides all the details necessary to let you manage large sets of data with confidence and ease. Its information-packed pages demonstrate how to use the two types of filters provided by Excel: AutoFilters and advanced filters.
Check out ExcelTips: Filters and Filtering today!
PivotTables don't need to be scary or mysterious. Use this powerful tool to analyze your data in ways you didn't know were possible. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site