Hiding a Selection


Posted by Phil D. Gonzalez on January 04, 2002 7:36 AM

Need some VBA help with this one.

What I need done is to somehow "hide" a selected range (see current code below).

---------------------
Dim CellRef As Range

For Each CellRef In Range("Q4:Q1202")
If CellRef.Value = "Y" Then
Range(CellRef.Address).EntireRow.Select
'Need to somehow hide this selected range
End If
Next
---------------------

The reason for wanting to hide the selection rather than using "CellRef.EntireRow.Hidden = True" is because the tested cell is actually merged, and what I actually need hidden is 2 rows (the row where the "Y" value resides, and the row immediately after that).

I found that by "selecting" the entire row where the merged cell resides, that both rows I need hidden are selected. So thus the need for hiding the "selection".

If there is an easier way to get the end result (both rows hidden) then I'll be glad to hear it.

Posted by Phil D. Gonzalez on January 04, 2002 7:50 AM

Actually, just got this working by changing the code to:

--------------------
Dim CellRef As Range

For Each CellRef In Range("Q4:Q1202")
If CellRef.Value = "Y" Then
Rows.Range(CellRef.Address).EntireRow.Select
Selection.EntireRow.Hidden = True
End If
Next
--------------------

But if there's an easier way to do this, I'd still like to hear it. :)



Posted by Banquo on January 04, 2002 3:28 PM

For Each CellRef In Range("A1:A10")
If CellRef.Value = "Y" Then
CellRef.MergeArea.EntireRow.Hidden = True
End If
Next

(Have a look at MergeCells in the VBE Help file)