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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobinorlandoAuthor Commented:
Thanks!
AshokSr. Software EngineerCommented:
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

AshokSr. Software EngineerCommented:
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
AshokSr. Software EngineerCommented:
=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")
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
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?
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
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?
AshokSr. Software EngineerCommented:
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")
AshokSr. Software EngineerCommented:
Good Year 2014 is coming soon.
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
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!
AshokSr. Software EngineerCommented:
That's good to know!
rspahitzCommented:
Barry, you're correct...I just wasn't looking carefully enough.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.