
Loop Exit after Last Find
Maybe not each line....(and interspersed)
Quote:
> Thanks, Dave Peterson! This code does everything I wanted, and more!
> However, I'd like to understand the code a little better. Could someone
> pretend that I've never seen VBA code before, and please annotate each
> line?
> Thanks very much.
> JE's answer will be much quicker if your named range is one column, but
> here's one way out of the looping when you're doing find's (slightly
> modified from VBA's Help for .find):
The option explicit forces me to declare (with Dim lines) each of my variables.
Quote:
> Option Explicit
> Sub testme03()
Some/most of these variables are range objects--like a cell or a group of cells
in the worksheet. One is a string (just a bunch of letters/numbers/special
characters).
Quote:
> Dim FoundCell As Range
> Dim FirstAddress As String
> Dim myRange As Range
> Dim HideMe As Range
use a variable that refers to the named range on sheet1. By using a variable,
you can make changes to the code easier (usually). By using variables that are
dimmed appropriately, you get the intellisense (autocomplete) when you're
writing the code, too!
Quote:
> Set myRange = Worksheets("sheet1").Range("myNamedRange")
The with statement means each thing that starts with a dot (.) has that thing
prefixed to it. So .find("~~").... could also be written as:
myrange.find("~~")......
It saves typing and makes it easier to update (I think).
Quote:
> With myRange
Look for ~ (this is a special character so we have to double up on it. To find
an asterisk--not the wildcard, but the asterisk, you have to use ~* (same with
?, ~?). Same with tilde.
This is like hitting ctrl-F (or edit|Find) after selecting myNamed range.
Quote:
> Set FoundCell = .Find("~~", LookIn:=xlValues)
This checks to see if it actually found anything. In excel, you'd get a message
saying it wasn't found. In VBA, you have to check yourself. (If it wasn't
found, that foundcell variable is never set. So it's still nothing.)
Quote:
> If Not FoundCell Is Nothing Then
But if it was found, keep track of the first one you found--use the address.
Quote:
> FirstAddress = FoundCell.Address
Do this loop until ~ isn't found or until the one you found has the same address
as the first one you found.
Quote:
> Do
Keep track of the cells that you found in a range "accumulator". If it's never
been set, set it to the firstcell found. If it has been set, then just merge
that next found cell into that accumulator range.
Quote:
> If HideMe Is Nothing Then
> Set HideMe = FoundCell
> Else
> Set HideMe = Union(FoundCell, HideMe)
> End If
Look again!
Quote:
> Set FoundCell = .FindNext(FoundCell)
end of the do loop
Quote:
> Loop While Not FoundCell Is Nothing _
> And FoundCell.Address <> FirstAddress
> End If
> End With
Check the accumulator range. If you never added anything to it, don't do
anything.
Quote:
> If HideMe Is Nothing Then
> 'do nothing
> Else
If you did find something, then hide every row in that range.e
Quote:
> HideMe.EntireRow.Hidden = True
> End If
> End Sub
> (you just keep finding the same thing and checking the address against the
> the address of the first one you found. When the current one matches the
> first one, you know you've gone through them all.)
> --
> David Godinger: Go player, student of Mahatma Gandhi, the Buddha, and Dr. Martin King
--
Dave Peterson