Determine if item is in a range of items
On Thu, 28 Feb 2008 05:08:01 -0800, Mark
wrote:
I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.
You can use the VBA FIND method:
==================
Sub foo()
'"Rng" is a named range on the active worksheet
'containing the list of items
Const LookFor As String = "dog"
Dim ItemPresent As Boolean
If Range("Rng").Find(LookFor) Is Nothing Then
ItemPresent = False
Else
ItemPresent = True
End If
Debug.Print LookFor, ItemPresent
End Sub
==============
So in your case
LookFor = [A1].value
and
you could substitute "B1:B10" for Rng
--ron
|