Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF a cell contains a string

How can I assign a value to cells if it's neighbour contains a specific string?

For example, fields in column A:

    dog11     cat22     cow11     chick11     duck22     cat11     horse22     cat33 

The syntax in column B would be:

=IF(SEARCH("cat",A1),"cat",IF(SEARCH("22",A1),"22","none")) 

It always picks up the first TRUE cell, but drops when the value is not true.

like image 963
Csongor Avatar asked Aug 08 '12 15:08

Csongor


People also ask

How do you write a formula if a cell contains certain text?

If cell contains specific text, then return a value Select the output cell, and use the following formula: =IF(cell="text", value_to_return, ""). For our example, the cell we want to check is A2, the text we're looking for is “example”, and the return value will be Yes.


1 Answers

SEARCH does not return 0 if there is no match, it returns #VALUE!. So you have to wrap calls to SEARCH with IFERROR.

For example...

=IF(IFERROR(SEARCH("cat", A1), 0), "cat", "none")

or

=IF(IFERROR(SEARCH("cat",A1),0),"cat",IF(IFERROR(SEARCH("22",A1),0),"22","none"))

Here, IFERROR returns the value from SEARCH when it works; the given value of 0 otherwise.

like image 53
shipr Avatar answered Sep 21 '22 05:09

shipr