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: Skipping Hidden Rows in a Macro.

Skipping Hidden Rows in a Macro

by Allen Wyatt
(last updated November 9, 2015)

4

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 (2286) 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: Skipping Hidden Rows in a Macro.

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

Protecting Styles

If you spend a lot of time getting your document styles set "just right," you don't want to take the chance that they will be ...

Discover More

Friendly and Informative Error Handling

When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's one ...

Discover More

Printing Row Numbers

Excel displays row numbers on-screen that help you easily see what is in each row. If you want to print these row numbers, ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Simultaneous Scrolling

If you have two worksheets displayed at the same time, you might want those worksheets to remain visually "in sync" with each ...

Discover More

Pulling Cell Names into VBA

Excel allows you to define names that can refer to either ranges of cells or to constant information, such as formulas. If ...

Discover More

Converting Text Case

Ever notice that if someone types in all CAPS, it appears they are shouting? If your worksheets include lots of text, you may ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 0 + 7?

2016-12-28 03:35:33

juzer

I have VBA code to highlight duplicate entry , but this highlight also the number which is in hidden criteria , can you advice a new code, below is the code

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Row = 1 Then Exit Sub ' IF ITS A HEADER, DO NOTHING.

On Error GoTo ErrHandler
Application.ScreenUpdating = False

Dim myDataRng As Range
Dim cell As Range

' WE WILL SET THE RANGE (SECOND COLUMN).
Set myDataRng = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)

For Each cell In myDataRng
cell.Offset(0, 0).Font.Color = vbBlack ' DEFAULT COLOR.

' LOCATE DUPLICATE VALUE(S) IN THE SPECIFIED RANGE OF DATA.
If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then
cell.Offset(0, 0).Font.Color = vbRed ' CHANGE FORE COLOR TO RED.
End If
Next cell

Set myDataRng = Nothing
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub


2016-05-04 10:28:06

Massogfredo

Thank you!


2015-11-27 17:04:01

Dennis Chua

Thank you so much for your tips. Do you also have tips on Outlook?


2015-04-19 09:20:52

Mehdi

thank you


This Site

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.

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.