Formula to seek text in adjacent range and return first matching text

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
669
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
I'm trying to come up with a formula that takes an arbitrary number of string arguments, then checks for the presence of any of those strings in a range of adjacent cells (same row) and returns the first (i.e. left-to-right) match, if it finds one, or simply nothing if there are no such matches.

So for example :

ABCDEFG
AppleOrangeLemonGrapefruitKiwiBanana
WatermelonLemonStrawberryGrapeRaspberryApple
CherryKiwiBananaMangoLimeApple

Let's say my string arguments are "Orange", "Banana" and "Mango"

I need a formula in column A that returns the first match in columns B through G for any of those strings :

ABCDEFG
OrangeAppleOrangeLemonGrapefruitKiwiBanana
WatermelonLemonStrawberryGrapeRaspberryApple
BananaCherryKiwiBananaMangoLimeApple

Row 1 contains both "Orange" (column C) and "Banana" (column G) so the formula returns "Orange" as it is the first match against any of the argument strings when looking left-to-right
Row 2 doesn't contain any of "Orange", "Banana" or "Mango" so the formula returns nothing (blank / zero length string)
Row 3 contains both "Banana" (column D) and "Mango" (column E) so the formula returns "Banana" as it is the first match against any of the argument strings when looking left-to-right

Thought I could do this quite easily with some kind of INDEX/MATCH or FIND/SEARCH but struggling to come up with one (the array of arguments is what's hurting me there, as opposed to a single atomic lookup)

(Apologies for the laboured example by the way, this was the simplest way I could explain what it is exactly I'm trying to do!... o_O)

Thanks in advance!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi, for XL365, you could try:

Book10
ABCDEFG
1OrangeAppleOrangeLemonGrapefruitKiwiBanana
2 WatermelonLemonStrawberryGrapeRaspberryApple
3BananaCherryKiwiBananaMangoLimeApple
Sheet1
Cell Formulas
RangeFormula
A1:A3A1=LET(lookfor,"Orange,Banana,Mango",lookin,B1:H1,TAKE(FILTER(lookin,ISNUMBER(MATCH(lookin,TEXTSPLIT(lookfor,","),0)),""),,1))
 
  • Like
Reactions: AOB
Upvote 0
Hi, for XL365, you could try:

Book10
ABCDEFG
1OrangeAppleOrangeLemonGrapefruitKiwiBanana
2 WatermelonLemonStrawberryGrapeRaspberryApple
3BananaCherryKiwiBananaMangoLimeApple
Sheet1
Cell Formulas
RangeFormula
A1:A3A1=LET(lookfor,"Orange,Banana,Mango",lookin,B1:H1,TAKE(FILTER(lookin,ISNUMBER(MATCH(lookin,TEXTSPLIT(lookfor,","),0)),""),,1))

Well that's my mind blown @FormR thanks for that!

Only one problem though - unfortunately, my string arguments can contain commas, so I can't define "lookfor" as a comma-delimited list of strings... 😭
 
Upvote 0
Hi, you can choose any delimiter and specify it here.

....MATCH(lookin,TEXTSPLIT(lookfor,","),0)),"...
 
Upvote 0
you can choose any delimiter and specify it
for example..
Book10
ABCDEFG
1OrangeAppleOrangeLemonGrapefruitKiwiBanana
2 WatermelonLemonStrawberryGrapeRaspberryApple
3BananaCherryKiwiBananaMangoLimeApple
4com,maCherrycom,ma
Sheet1
Cell Formulas
RangeFormula
A1:A4A1=LET(lookfor,"Orange|Banana|Mango|com,ma",lookin,B1:H1,TAKE(FILTER(lookin,ISNUMBER(MATCH(lookin,TEXTSPLIT(lookfor,"|"),0)),""),,1))
 
  • Like
Reactions: AOB
Upvote 0
Solution
for example..
Book10
ABCDEFG
1OrangeAppleOrangeLemonGrapefruitKiwiBanana
2 WatermelonLemonStrawberryGrapeRaspberryApple
3BananaCherryKiwiBananaMangoLimeApple
4com,maCherrycom,ma
Sheet1
Cell Formulas
RangeFormula
A1:A4A1=LET(lookfor,"Orange|Banana|Mango|com,ma",lookin,B1:H1,TAKE(FILTER(lookin,ISNUMBER(MATCH(lookin,TEXTSPLIT(lookfor,"|"),0)),""),,1))

Absolutely wonderful! This is so awesome, thank you - works a charm but will play around with this for other uses too.

(Should've looked further down the formula for the TEXTSPLIT function that would have made me realise this myself, apologies!... 🤦‍♂️ )
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top