Excel - Search string for any one of multiple values

Experts, I have a column (Column "O") of text strings in excel and the text in some rows contains a year e.g. 2000.
Years could be anything from 2000 to 2010.

What I want to do is write a formula in an adjacent column (Column "N") that will search the text strings in Col O for the occurrence of any of a number of designated year values and if one of those values appears, write something like "Bad Year" in the adjacent column and if not a match, leave the cell blank.

Here's what I tried but excel does not like it:

=IF(ISNUMBER(SEARCH("2000",o2)) OR ISNUMBER(SEARCH("2001",o2)) OR ISNUMBER(SEARCH("2002",o2)) OR ISNUMBER(SEARCH("2003",o2)) OR ISNUMBER(SEARCH("2004",o2)) OR ISNUMBER(SEARCH("2005",o2)) OR ISNUMBER(SEARCH("2006",o2)) OR ISNUMBER(SEARCH("2007",o2)) OR ISNUMBER(SEARCH("2008",o2)) OR ISNUMBER(SEARCH("2009",o2)) OR ISNUMBER(SEARCH("2010",o2)) OR ISNUMBER(SEARCH("2011",o2)),"Bad Year","")

What is a better way to do this?

Thanks in advance.
LVL 1
bobinorlandoAsked:
Who is Participating?
 
AshokConnect With a Mentor Commented:
When typing in notepad, make sure it is one continuous line (without a line feed).

=IF(((ISNUMBER(SEARCH("2000",O2)))*OR(ISNUMBER(SEARCH("2001",O2)))*OR(ISNUMBER(SEARCH("2010",O2)))*OR(ISNUMBER(SEARCH("2011",O2)))),"","Bad Year")

Add more 2002 to 2009 in above string.

HTH
Ashok
0
 
bobinorlandoAuthor Commented:
Thanks!
0
 
AshokCommented:
That may not work

=IF(OR(ISNUMBER(SEARCH("2000",O3)),ISNUMBER(SEARCH("2000",O3))),"","Bad Year")

In above there are only 2 conditions.

I would suggest add one condition at a time and make sure it is working.

HTH
Ashok
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
rspahitzCommented:
How about using wildcards like this?
SEARCH("20??",O2)

It will actually give you more than you need, so maybe that's not the way you want to go...
but you could also extend it to "200?" or "2010":

=IF(OR(ISNUMBER(SEARCH("200?",O2)), ISNUMBER(SEARCH("2010",O2))), "Bad Year","")

Open in new window

0
 
AshokCommented:
Here is 5 conditions.

=IF(OR(ISNUMBER(SEARCH("2000",O3)),ISNUMBER(SEARCH("2001",O3)),ISNUMBER(SEARCH("2002",O3)),ISNUMBER(SEARCH("2003",O3)),ISNUMBER(SEARCH("2004",O3))),"","Bad Year")

Above is tested OK.

Thanks
0
 
AshokCommented:
=IF(OR(ISNUMBER(SEARCH("2000",O2)),ISNUMBER(SEARCH("2001",O2)),ISNUMBER(SEARCH("2002",O2)),ISNUMBER(SEARCH("2003",O2)),ISNUMBER(SEARCH("2004",O2)),ISNUMBER(SEARCH("2005",O2)),ISNUMBER(SEARCH("2006",O2)),ISNUMBER(SEARCH("2007",O2)),ISNUMBER(SEARCH("2008",O2)),ISNUMBER(SEARCH("2009",O2)),ISNUMBER(SEARCH("2010",O2)),ISNUMBER(SEARCH("2011",O2))),"","Bad Year")
0
 
barry houdiniCommented:
You can do this with one SEARCH function like this

=IF(COUNT(SEARCH({2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011},O2)),"Bad Year","")

or even shorter like this:

=IF(COUNT(SEARCH(2000+{0,1,2,3,4,5,6,7,8,9,10,11},O2)),"Bad Year","")

regards, barry
0
 
rspahitzCommented:
Good point Barry.

I guess we can even shorten it to this:

=IF(ISERR(SEARCH({"200?","2010"},O2)),"","Bad year")

Oh Bob, I see in your post that it seems that you want 2000-2010 to be "bad year" (which is how my code works).  Is that correct or are those the "good years"?
Also, part of your post says to use the range 2000-2010 but your code seems to include 2011.  I guess that's easy enough to adopt, but which one is correct?
0
 
barry houdiniCommented:
That's not going to work, as is, Rob!

When you search for multiple values in the { and } braces SEARCH will return an array and so will ISERR, but in your suggested formula you have no function that handles that array, so it works for 2003 but not 2010.

You can use AND to handle that, i.e.

=IF(AND(ISERR(SEARCH({"200?","2010"},O2))),"","Bad Year")

In my version COUNT is different, that function returns a single result from an array, so you can use this version again with COUNT:

=IF(COUNT(SEARCH({"200?","2010"},O2)),"Bad Year","")

...or go back to OR with ISNUMBER, i.e.

=IF(OR(ISNUMBER(SEARCH({"200?","2010"},O2))),"Bad Year","")

All of those should return the correct result assuming 2000 to 2010  only are "bad years"

regards, barry
0
 
rspahitzCommented:
thanks Barry...not sure why it works fine on my machine (Excel 2007), but COUNT seems like a worthwhile alternative.

Here's the data I'm testing in A1:A20 :
abc1996xyz
abc1997xyz
abc1998xyz
abc1999xyz
abc2000xyz
abc2001xyz
abc2002xyz
abc2003xyz
abc2004xyz
abc2005xyz
abc2006xyz
abc2007xyz
abc2008xyz
abc2009xyz
abc2010xyz
abc2011xyz
abc2012xyz
abc2013xyz
abc2014xyz
abc2015xyz

starting with this formula:
=IF(ISERR(SEARCH({"200?","2010"},A1)),"","Bad year")
B5:B15 correctly show a message and all the rest are empty.

Are you saying that it won't work if there are multiple matches?
0
 
AshokCommented:
By the way, first should be true which empty string and second is false so "Bad Year".

I prefer this as you can easily see all values.

=IF(COUNT(SEARCH({2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011},O2)),"","Bad Year")

or

=IF(COUNT(SEARCH(2000+{0,1,2,3,4,5,6,7,8,9,10,11},O2)),"","Bad Year")
0
 
AshokCommented:
Good Year 2014 is coming soon.
0
 
barry houdiniCommented:
>B5:B15 correctly show a message and all the rest are empty.

Are you sure, Rob? I get B15 empty, although A15 contains 2010

That's what I would expect because in this formula

=IF(ISERR(SEARCH({"200?","2010"},A15)),"","Bad year")

Where A15 = abc2010xyz

You get SEARCH returning this array:

{#VALUE!,4}

and then when you apply ISERR to that array you get this array

{TRUE,FALSE}

....but as I said you have no function that evaluates that array so typically the IF function uses the first value, TRUE, so you get a blank instead of "bad year".

Your results are the same as if you used this formula

=IF(ISERR(SEARCH("200?",A1)),"","Bad year")

if I add an AND as per my suggestion above, i.e.

=IF(AND(ISERR(SEARCH({"200?","2010"},A1))),"","Bad Year")

the function it works as required  because

=AND({TRUE,FALSE}) = FALSE

All other years work because they are picked up by the "200?" criterion

regards, barry
0
 
bobinorlandoAuthor Commented:
Hmm I attempted to accept this as the solution earlier today. It didn't seem to take. Anyway I found Ashok's solution to work with a minor modification - I changed * to +.
Thanks to the other experts for chiming in!
0
 
AshokCommented:
That's good to know!
0
 
rspahitzCommented:
Barry, you're correct...I just wasn't looking carefully enough.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.