Test if cell value is in an array

R

Rita Brasher

I want to find out if the contents of a cell are in a predetermined
list. I don't want to have to keep writing an "If" statement for each
entry in the list.

Example:

Array = (Apples, oranges, grapes, bananas}

I want to measure the contents of the cell to determine if they are in
my array. If yes, put a 1, if no put 0. (Will sum 1s later for totals)


I know there's a relatively simple way to do this, but I can't seem to
grasp it right now.

Thanks in advance for any help you can provide!!
Rita
 
G

Gary''s Student

How about:

Function within(r As Range) As Integer
within = 0
s = Array("Apples", "oranges", "grapes", "bananas")
v = r.Value
For i = 0 To 3
If v = s(i) Then
within = 1
End If
Next
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top