Google Sheets IF/Find formula help

  • Thread starter Thread starter Greg Bernhardt
  • Start date Start date
  • Tags Tags
    Formula Google
Click For Summary
To check if cell A11 contains the word "tools" and display corresponding messages in B11, the use of IF() and FIND() functions is common. However, FIND() generates an error if the substring is not found, which can be handled using IFERROR(). A more efficient approach is to use ISERROR() or ISNUMBER() with FIND(), allowing for a cleaner output without errors. The formulas =IF(ISERROR(FIND("tools",A11)),"No Tools","Yes Tools") and =IF(ISNUMBER(FIND("tools",A11)),"Yes Tools","No Tools") effectively return the desired results. While both methods work, ISNUMBER() is suggested as a better practice. The discussion highlights a limitation in Excel and similar software, where substring searches throw errors instead of returning a boolean or numeric value, contrasting with programming languages like Java that handle such searches more gracefully.
Messages
19,851
Reaction score
10,882
TL;DR
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
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
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
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
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
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
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...

Similar threads

Replies
10
Views
5K
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 26 ·
Replies
26
Views
3K
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
10
Views
2K