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: Skipping Hidden Rows in a Macro.
by Allen Wyatt
(last updated February 10, 2018)
When using a worksheet, it is not uncommon to hide rows that contain data you don't want displayed at the current time. If you have written a macro that processes the data in the worksheet, you may have wondered how to skip over and not process the rows that you have marked as hidden.
The way you accomplish this is to check the Hidden property of each row. If the property is True, then the row is hidden; if False, then row is visible.
As an example of how this works, assume that you have a worksheet that you use to track clients. Some of these clients are considered active and others inactive. To mark a client as inactive, you hide the row containing the client. At some point, you want to number the active clients, and you want to do it using a macro. The following macro will do the trick for you:
Sub NumberClients() Dim c As Range Dim j As Integer If Selection.Columns.Count > 1 Then MsgBox "Only select the cells you want numbered" Exit Sub End If j = 0 For Each c In Selection If Not c.Rows.Hidden Then j = j + 1 c.Value = j Else c.Clear End If Next c End Sub
To use the macro, simply select the cells in which the numbering will be done. The macro checks, first of all, to make sure you have only selected cells in a single column. Then, it steps through each cell in the selected range. If the row containing the cell is not hidden, then the counter (j) is incremented and stored in the cell. If the row containing the cell is hidden, then the contents of the cell are cleared. The key to this macro is the If ... End If structure that tests the value of the Hidden attribute.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12552) 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: Skipping Hidden Rows in a Macro.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When you need to stop a macro while it is running, you normally press Ctrl+Break. What are you to do if the keypress ...Discover More
When processing information in a macro, you often need to select different cells relative to the currently selected ...Discover More
If you need to know whether a particular value is odd or even, you can use this simple formula. Designed to be used in 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.