155

I'm trying to figure out how to identify IF a list of items in one cell containing a value or string.

EXAMPLE

Cell A1 contains sites, sheets, docs, slides.

I want cell B1 to display a 1 'if' cell A1 contains the string sites.

FORMULA

=if(A1 ?????? "sites", 1,0)

I'm unsure what to replace the ?????? within the above formula OR if this formula is possible. Any ideas on how to accomplish the desired outcome are greatly appreciated.

1

9 Answers 9

190

You can use REGEXMATCH:

=IF(REGEXMATCH(A1, "sites"), 1, 0)

To explain, REGEXMATCH returns true if and only if the argument is a substring of your string.

14
  • 13
    Using regexmatch would be simpler: if(regexmatch(A1, "sites"), 1, 0).
    – user79865
    Commented Jul 19, 2016 at 1:09
  • 5
    The "if" isn't needed since regexmatch already returns a boolean, so just "REGEXMATCH(A1, "sites")"
    – David
    Commented Jan 18, 2019 at 17:17
  • 3
    The if is there so they can convert the true to a 1 or 0 if false Commented Jan 18, 2019 at 17:18
  • 2
    If parameters should be semicolon, not comma: =IF(REGEXMATCH(A1, "sites"); 1; 0) Commented Dec 17, 2019 at 12:35
  • 2
    "REGEXMATCH returns true if and only if the argument is a substring of your string." this is a highly misleading statement. Strings are interpreted as regular expressions so there are many cases where this does not work.
    – AnnanFay
    Commented Jun 17, 2020 at 19:11
30

If you, as Ryan Shillington suggests, want to know the number of cells within a range that has "sites" in it, you might try:

=COUNTIF(A1:A100;"*sites*") 

The * acts as a wildcard notion, so it will look for a match anywhere within the cell, not just an exact match for the whole cell.

I've also done this with a cell reference instead of "sites", but then you have to add wildcard-asterixes to the text in the cell.

2
  • 1
    I haven't tested it but I expect you could add the asterisks in the formula when referencing a cell by doing =COUNTIF(A1:A100; CONCATENATE("*", B1, "*"))
    – Matt
    Commented Dec 17, 2020 at 18:43
  • I find this to be the simplest solution and can confirm that it works. Commented Mar 1, 2021 at 17:49
22

This returns a #VALUE! error if the search term isn't found:

=IF(SEARCH("sites", B2) > 0, 1, 0)

This uses the same logic but catches the error and returns a 1/0 for success/failure:

=IF(IFERROR(SEARCH("sites", B2) > 0, 0), 1, 0)
2
  • 5
    The first formula will never return 0 because SEARCH(search_for, text_to_search, [starting_at]) will return an error when search_for do not be found. Commented Jul 15, 2016 at 23:05
  • 1
    How do I use search with COUNTIF? Commented Feb 8, 2020 at 0:10
11

A solution without REGEX:

=if(iserror(FIND("sites",B2)),0,1)

capitalization matters and if logic must be reversed, given that answer is true when the string is not found.

1
  • This is basically the same as this answer, but introducing the case sensitive thing. By the way the sample data provided by the OP includes sheets not Sheets. Commented Jan 16, 2019 at 19:04
5

This will return TRUE or FALSE:

=IFERROR(SEARCH("keyword",A1)>0, FALSE)

4

You're probably doing this because you want to know the count or average # of rows that have the word "sites" in it. Assuming you have 100 rows of entries in column A, you can use this function to find out how many rows have the word "sites" in it:

=QUERY(A1:A100, "select count(A) where A like '%sites%'")

This is much faster than creating a column of 1s and zeros and then operating on that new column.

2
  • Can this work over a box instead of just a column? Commented Sep 11, 2019 at 18:53
  • Yup. instead of A1:A100 you could make it A1:C100 and then write a query with columns A, B and C. Commented Sep 11, 2019 at 19:30
3

If you go with the asterisks option and want a cell reference, you can use the CONCATENATE function to add the asterisks

=COUNTIF(A1:A100,CONCATENATE("*",A1,"*"))

1
  • CONCATENATE can be harder to read than using & to join the strings: =COUNTIF(A1:A100, "*" &A1 &"*"))
    – RedYeti
    Commented Feb 13, 2021 at 8:46
1

=IF(QUERY(A1, "where A contains 'sites'")<>"", 1, )

0

0

Countif is a memory hog and a lot of these answers depend on not true type approach (iferror etc.). While that works, in a general better approach to conditions is seeking what is true.

Case not sensitive:

=isnumber(SEARCH("sites",A1))

Case sensitive:

=isnumber(SEARCH("sites",A1))

This will return TRUE if a match, or FALSE if not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.