View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ed[_9_] Ed[_9_] is offline
external usenet poster
 
Posts: 194
Default Constrain to visible area?

Tom:

I used your second example, and it works perfectly! I can filter to a
general catagory, then search for my string only within the filtered range.
Many thanks!

For information, I'm assuming the AutoFilter does not "hide" rows? And so
your first example would not work on a filtered range because it would not
detect any hidden rows? Is this correct?

Ed

"Tom Ogilvy" wrote in message
...
Sub SelectiveRowHide()

Dim myTarget As String
Dim myFind As Range
Dim i As Integer

myTarget = Application.InputBox("What text are you searching for?")

For i = 2 To Range("A65536").End(xlUp).Row
if not rows(i).Hidden then
Rows(i).Select
Set myFind = Rows(i).Find(What:=myTarget)
If myFind Is Nothing Then
Selection.EntireRow.Hidden = True
End If
end if
Next i

End Sub


would be one interpretation of what you said. If you mean only search the
visible cells in the filtered range

Sub SelectiveRowHide()

Dim myTarget As String
Dim myFind As Range
Dim i As Integer
Dim rng as range

myTarget = Application.InputBox("What text are you searching for?")
If activesheet.Autofiltermode then
set rng = Activesheet.Autofilter.Range
Else
set rng = Range(cells(2,1),cells(rows.count,1).End(xlup))
End if
For each cell in rng.specialcells(xlvisible)
i = cell.row
if not rows(i).Hidden then
Rows(i).Select
Set myFind = Rows(i).Find(What:=myTarget)
If myFind Is Nothing Then
Selection.EntireRow.Hidden = True
End If
End if
Next cell
End Sub

Regards,
Tom Ogilvy


"Ed" wrote in message
...
I have a macro that searchs for a text string and hides any row not
containing the string (code at the end). (I got this from a post on a

NG,
and tried a Google search so I could give big credits to the original
author, but I couldn't find the original post.) It works great, with

one
small hitch.

If I filter my worksheet to show a range selected from the middle of the
sheet, because it starts from 2, it will search even what is not

visible.
Would it be difficult to add some lines to constrain the search area to

only
what is visible after filtering? It also does this if I try to run it
twice.

Sub SelectiveRowHide()

Dim myTarget As String
Dim myFind As Range
Dim i As Integer

myTarget = Application.InputBox("What text are you searching for?")

For i = 2 To Range("A65536").End(xlUp).Row
Rows(i).Select
Set myFind = Rows(i).Find(What:=myTarget)
If myFind Is Nothing Then
Selection.EntireRow.Hidden = True
End If
Next i

End Sub


Ed