try this:
=IFERROR(INDEX(D$2:D$5,MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2)),0,1),0),1),"District not in list")
Entered as array formula by pressing CTRL
+SHIFT
+ENTER
.
Enter this adjacent to your first address.(below it is in B2
and address is on A2
)
Change it to suit your need.
I assumed that your data looks like this.
As you can see, I put the District List on D2:D5
so in the formula, change it to where your list is.
If the no match is found, it will return District not in list
as seen in B6
.
I believe every address have a unique Disrtict?
If ever the adress contains two(2) Districts, it will return the first found District in the address.
How does the formula work?
=SEARCH(D$2:D$5,A2)
This searches each word found in D2:D5
(your districts) in address in A2
.
The result is an array of error values and numbers.
Considering A2
it returns {#Value!, #Value!, 14, #Value!}
.
To handle the errors we add ISERROR
combined with IF
.
=IF(ISERROR(SEARCH(D$2:D$5,A2),0,1)
This replaces #VALUE!
with 0
and non-error value with 1
.
Now it returns {0, 0, 1, 0}
.
Now, you want to return the District of the corresponding address.
To do that we will use INDEX
and MATCH
combination.
First we use MATCH
to know the row number of our District.
We already have this array {0, 0, 1, 0}
, we need to match 1
.
=MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2),0,1),0)
Simplified:
=MATCH(1,{0, 0, 1, 0}, 0)
And yields 3
.
Finally, we use INDEX
to return the District
of the corresponding address.
=INDEX(D$2:D$5,MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2)),0,1),0),1)
Simplified:
=INDEX(D$2:D$5,3,1)
The last argument which is [column]
can be omitted since you only have 1.
Or you can specify it as 1
.
This yields to Warangal
.
Note that we added IFERROR
to handle non-existing District.
Hope this helps you a bit.