Google Sheets IF/Find formula help

In summary: They both work and are both commonly used. It really comes down to personal preference. If you feel like one is more efficient or easier to understand, then go with that one.
  • #1
19,443
10,021
TL;DR Summary
Trying to efficiently create a formula to find string in a cell and if true then return a string.
Say cell A11 contains the text "power tools". I want B11 to display if A11 contains the word "tools".

I choose to use IF() and FIND(). If "tools" does exist it returns "Yes Tools" fine, but if not it returns an error saying FIND() didn't match a value instead of displaying "No Tools". I got around this by wrapping it all in IFERROR(). This works, but I can't imagine it's efficient and there must be a better way.

Code:
=IFERROR(IF(FIND("tools",A11),"Yes Tools","No Tools"), "No Tools")
 
Last edited:
Computer science news on Phys.org
  • #2
FIND() never returns a false value so there is no point using it directly in an IF statement, instead use:
Code:
=IF(ISERROR(FIND("tools",A11)),"No Tools","Yes Tools")
(as an aside, in environments with zero indexed strings your code would return "No Tools" if the string in A11 started with "tools" - a common bug caused by lazy programming and inadequate unit testing)
 
  • Informative
Likes Greg Bernhardt
  • #3
Both FIND() and SEARCH() return the location of substring if the search is true, otherwise a value error. So try IF(ISNUMBER(FIND("tools",A11)),"Yes Tools","No Tools").
 
  • Informative
Likes Greg Bernhardt
  • #4
Thanks! Both of those work! Perhaps ISNUMBER() is better best practice?

Code:
=IF(ISERROR(FIND("tools",A11)),"No Tools","Yes Tools")

Code:
=IF(ISNUMBER(FIND("tools",A11)),"Yes Tools","No Tools")
 
Last edited:
  • Like
Likes Wrichik Basu
  • #5
Greg Bernhardt said:
Perhaps ISNUMBER() is better best practice?
Basically none of these approaches is good from a programmer's point of view. When I want to search for a substring within a string, I would want the function to either return a boolean value, or a numeric value. For example, in Java, contains() returns boolean, while indexOf() returns -1 if the substring is absent. But they don't throw an exception if the search is unsuccessful. This is something found only in Excel/Google sheets and similar software.
 
  • Informative
Likes Greg Bernhardt
  • #6
Greg Bernhardt said:
Thanks! Both of those work! Perhaps ISNUMBER() is better best practice?
I can't see any reason to prefer either over the other.
 
  • Like
Likes Greg Bernhardt

What is the IF/Find formula in Google Sheets?

The IF/Find formula in Google Sheets is a combination of the IF and FIND functions. It allows you to search for a specific text within a cell and return a different value if the text is found or not.

How do I use the IF/Find formula in Google Sheets?

To use the IF/Find formula, you need to follow the syntax: =IF(FIND(search_text, text_to_search)>0, value_if_true, value_if_false). First, specify the text you want to search for in the search_text argument. Then, specify the cell or range of cells you want to search within in the text_to_search argument. Lastly, provide the value you want to return if the text is found in the value_if_true argument and the value you want to return if the text is not found in the value_if_false argument.

What is the purpose of the IF/Find formula in Google Sheets?

The IF/Find formula is useful for conditional formatting in Google Sheets. It allows you to highlight cells or perform a specific action based on whether a certain text is found within a cell or not.

Can the IF/Find formula be used with multiple conditions?

Yes, the IF/Find formula can be used with multiple conditions by nesting it within another IF formula. This allows you to check for multiple search texts and return different values based on which text is found.

Are there any limitations to using the IF/Find formula in Google Sheets?

The main limitation of the IF/Find formula is that it can only search for a single text within a cell. It cannot be used to search for multiple texts or perform more complex searches. Additionally, the formula is case-sensitive, so it will only find exact matches.

Similar threads

Replies
10
Views
2K
Replies
6
Views
1K
  • Biology and Chemistry Homework Help
Replies
4
Views
1K
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
26
Views
592
Replies
2
Views
885
Replies
10
Views
960
  • Introductory Physics Homework Help
Replies
30
Views
505
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
Back
Top