- There's practiaclly no VBA code at all in the suggested approach posted. I assigned a Named Range to non-contiguous ranges of cells and referenced that named range in the macro.
Example:
To assign a Named Range to rows 1:4, 7:10, 15:16 and 22:24
• Select Rows 1:4
• Hold down the CTRL key while you then select:
...Rows 7:10
...Rows 15:16
...Rows 22:24
• Releast the CTRL key (rows 1:4, 7:10, 15:16 and 22:24 will all be selected)
• In the Name Box, in the upper left of the sheet, just above the Col_A header
...Type: MyRowsToHide
Now, this VBA command will hide those rows:
[MyRowsToHide].entirerow.hidden = TRUE
and this one will UNhide them:
[MyRowsToHide].entirerow.hidden = FALSE
The upside of that method is that you don't need to touch the VBA code when you need to change the rows to be hidden/unhidden. Just change the rows referenced in the Named Range.
- However, if you want all of the reference activity to happen within the code.
Try something like this to hide rows 1:4, 7:10, 15:16 and 22:24
Sub HideMyRows()
Dim MyRowsToHide
With ActiveSheet
Set MyRowsToHide = Application.Union(.Range("1:4"), .Range("7:10"), .Range("15:16"), .Range("22:24"))
End With
MyRowsToHide.EntireRow.Hidden = True
End Sub
Does that help?
Ron Coderre
Microsoft MVP (2006 - 2010) - Excel
P.S. If any post answers your question, please mark it as the Answer (so it won't keep showing as an open item.)