Formula - search cell for text and check if text is contained in a specified list

excelstarter1

Board Regular
Joined
Jul 20, 2017
Messages
81
Hello everyone,

I have a quick question. Using the FIND or SEARCH Function in Excel I can check wether a cell contains a specific content, e.g. =FIND("Denmark",A1) checks the cell A1 for the word Denmark and returns the position if the word is contained in the cell. Now I have a huge dataset with several entries. Each cell contains customer information, country, product etc. (I cannot seperate the info, there is no real logic behind it. e.g. one cell starts with the customer name, the other cell starts with the country). Now I want to extract the country information from each cell. My idea: I have a list of all countries in the world and want to check each cell content against this list of countries. As a result I want the formula to return a zero if nothing was found, or the name of the country if it is contained in the cell. So rather then saying "check the cell for the word, i.e. country, Denmark", I want to tell Excel "check each cell if one of the countries in my list is contained in the cell's content". I really don't have a clue how to approach this problem...

Would be great if one of you could help out!!

Thanks in advance!

Regards
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Re: Forumla - search cell for text and check if text is contained in a specified list

See if this very simple example helps


A
B
C
D
1
Text​
Result​
Countries​
2
John, Denmark, Product1​
Denmark​
Brazil​
3
Brazil, Product2, Mike​
Brazil​
Denmark​
4
Product3, Mary, France​
France​
France​
5
Product2, Richards​
0​
US​
6
Charles, US, ProductX​
US​

Formula in B2 copied down
=IFERROR(LOOKUP(9.99E+307,SEARCH(D$2:D$5,A2),D$2:D$5),0)

To understand how the formula works see this excellent explanation by Aladin Akyurek
Lookup for a value in Unsorted data

M.
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Awesome, thanks for the quick reply Marcelo!

Just wondering, how would you transform the formula such that I can use it as a vlookup function? So instead of returning the full name of the country, I want to return a number or an abbreviation, e.g. France = FR. First column full name of the country, second column abbreviation.

I read the explanation by Aladin Akyurek. Thanks for the reference! Is the "9.99E+307" term within the forumla really necessary, if I am only comparing text instead of numbers/values? To be honest, that was the part, I did not get :)
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

9.99E+307 is a big number (really big!). It's a short of 9.99999999999999E+307 that is the Largest allowed positive number

See the topic Calculation specifications and limits at
Excel specifications and limits - Excel

Aladin explains the role of this big number in the formula

To use country abbreviations try

A
B
C
D
E
1
Text​
Result​
Countries​
Abbr​
2
John, Denmark, Product1​
DE​
Brazil​
BR​
3
Brazil, Product2, Mike​
BR​
Denmark​
DE​
4
Product3, Mary, France​
FR​
France​
FR​
5
Product2, Richards​
0​
US​
US​
6
Charles, US, ProductX​
US​

<tbody>
</tbody>


Formula in B2 copied down
=IFERROR(LOOKUP(9.99E+307,SEARCH(D$2:D$5,A2),E$2:E$5),0)

M.
 
Last edited:
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

That works perfectly, thanks!

One issue I just came across: when a name or product contains the countries abbreviation, e.g. US (sUSan, rUSsia) or CH = Switzerland (CHarles), I obviously get a wrong match. Is there a way to check for exactly the right word. That is US or CH (Switzerland) has only two letters/characters. I could use this property to check that it is not surrounded by other letters/characters. So "xxxxxx xxx / CH xxxx" = Switzerland would be right, but "xxxx Charles, xxx" should return the result 0 as no country is mentioned; the result Switzerland would be wrong. Do you have an idea?

The problem is that the data I have contains the full country name as well as country abbreviations, so I have to be able to match CH to Switzerland as well as the full name Switzerland to Switzerland.

I hope my explanation was not too confusing...

Thanks!
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Please, provide a small data sample for testing purposes along with expected results

M.
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Original data
Assign country using Excel formulaRIGHT CountryWRONG Country
Robert XXXX, Product 1, Russia[FORMULA HERE]Russia-
Susan XXXX, Italy, Product 2[FORMULA HERE]ItalyUSA
China, William XXXX, Product 1[FORMULA HERE]ChinaSwitzerland
Denmark, Charles XXXX, Product 3[FORMULA HERE]DenmarkSwitzerland

<tbody>
</tbody>

Hope that helps. "Wrong country" means the current formula returns the wrong country.

My dataset for the countries looks like this:
Countries (compare with formula)
Show this country
Denmark
Denmark
Germany
Germany
France
France
CH
Switzerland
Italy
Italy
US
USA
USA
USA
...
...

<tbody>
</tbody>
 
Last edited:
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Are you using the full name (Russia, Italy,...) or abbreviations (RU, IT...) to search?

M.
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Both, as the original data (unfortunately) does include the full name as well as abbreviations...
 
Upvote 0
Re: Forumla - search cell for text and check if text is contained in a specified list

Try

A
B
C
D
E
1
Text​
Formula​
Countries​
Show​
2
Robert XXXX, Product 1, Russia​
Russia​
Denmark​
Denmark​
3
Susan XXXX, Italy, Product 2​
Italy​
Germany​
Germany​
4
China, William XXXX, Product 1​
China​
France​
France​
5
Denmark, Charles XXXX, Product 3​
Denmark​
CH​
Switzerland​
6
Charlotte xxxx, CH, Product 4​
Switzerland​
Italy​
Italy​
7
Richard xxxxx, Product 6​
0​
US​
USA​
8
US, ProductX, John​
USA​
USA​
USA​
9
Russia​
Russia​
10
China​
China​
11

Formula in B2 copied down
=IFERROR(LOOKUP(9.99E+307,SEARCH(","&D$2:D$10&",",","&SUBSTITUTE(A2," ","")&","),E$2:E$10),0)

M.
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,098
Members
449,205
Latest member
ralemanygarcia

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