Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Hiding Rows Based on Two Values.
Written by Allen Wyatt (last updated July 6, 2019)
This tip applies to Excel 97, 2000, 2002, and 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 an 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 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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2416) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Hiding Rows Based on Two Values.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Excel allows you to easily hide rows in a worksheet, so their contents are not visible. Figuring out how to detect where ...
Discover MoreWhen you format a cell so that the information within it can wrap to multiple lines, you may be surprised if Excel ...
Discover MoreWhen you enter information into a row on a worksheet, Excel automatically adjusts the height of the row based on what you ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments