0

I am trying to search an array of cells in excel to find if it contains a word to then further evaluate.

so for example; I have named the array (A1:A5) as 'cList'.

A1 = apple
A2 = pear
A3 = orange
A4 = banana
A5 = cherry

I want to

=SEARCH("pear",cList)

but i keep getting FALSE - which is not true because it is contained in A2. My thought here is that Search cannot be used on an array, because if I instead used

=SEARCH("pear",A2)

I will get my desired TRUE.

So is there another way to test an array if it contains and answer?

2 Answers 2

3

SEARCH only searches a single cell. The easiest way to find if a range contains a word is just to use COUNTIF

=COUNTIF($A$1:$A$5,"pear")

This tells you how many matches there are, or to get it as a TRUE/FALSE value

=COUNTIF($A$1:$A$5,"pear")>0

You can also use wildcards, so this would find things like "pearmain" and "prickly pear"

=COUNTIF($A$1:$A$5,"*pear*")>0
Sign up to request clarification or add additional context in comments.

1 Comment

completely forgot about COUNTIF() I knew it but I guess I was trying not to use yet another IF(), but it does the trick perfectly. Thanks Tom
0

Sounds like a for loop would work fine...

for(int i=1; i<6; i++){ String cell = "A" + i; if(SEARCH("pear",cell) //do things... }

EDIT: Rereading your question, you'd want to adapt this to loop over your array...

4 Comments

not really, the built in excel functions are way faster than re-implementing them in VBA... If you study CS, you should know to program against an api and use it's available methods instead of re-implementing. All you know you're asking every cell value, but Excel might store the data in some kind of hashmap and work much faster through the functions it exposes for you.
Absolutely, but another user covered that approach.
then your answer should include the fact that it will be slower, it would be a good quick/lazy fix or if someone wants to practice. You do say it would be "fine"
You're really over analyzing this. The OP determined the better answer on his own, no need to baby him

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.