Google Sheets IF/Find formula help

  • Thread starter Thread starter Greg Bernhardt
  • Start date Start date
  • Tags Tags
    Formula Google
Click For Summary

Discussion Overview

The discussion revolves around the use of the IF and FIND functions in Google Sheets to determine if a specific substring exists within a given text. Participants explore various methods to handle errors that arise when the substring is not found, and they consider best practices in formula design.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using IF and FIND to check for the substring "tools" in cell A11, but notes that FIND returns an error if the substring is not found, leading them to use IFERROR to handle this.
  • Another participant points out that FIND does not return a false value, recommending the use of ISERROR to check for errors instead.
  • A different participant proposes using ISNUMBER with FIND to determine if the substring exists, suggesting this as a more effective approach.
  • One participant expresses a preference for ISNUMBER as a potential best practice, while acknowledging that both ISERROR and ISNUMBER approaches work.
  • Another participant critiques the design of the functions, stating that they would prefer a boolean or numeric return value without exceptions, drawing a comparison to programming languages like Java.
  • One participant reiterates the effectiveness of both approaches but does not express a preference for one over the other.

Areas of Agreement / Disagreement

Participants generally agree that both ISERROR and ISNUMBER methods are valid for handling the FIND function's behavior, but there is no consensus on which method is superior or on the overall design of the functions in Google Sheets.

Contextual Notes

Participants mention potential issues with error handling and the behavior of functions in different programming environments, but these points remain unresolved in the context of Google Sheets.

Messages
19,910
Reaction score
10,923
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: Greg Bernhardt

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