View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default Determine if item is in a range of items

Mark,

You don't say what you want to do if you find it so this generates a
messagebox. Right click the sheet tab, view code and paste this in

Sub Mersible()
Dim MyRange As Range
Set MyRange = Range("B1:B10")
For Each c In MyRange
If c.Text = Range("A1").Text Then
MsgBox Range("A1").Text & " at " & c.Address
Exit Sub
End If
Next
MsgBox Range("A1").Text & " Not found "
End Sub

You correct about the one liner because this is easily dooable using a
worksheet function:-
=COUNTIF(B1:B10,A1)0

Mike
"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.