259

Is there an in-built function to check if a cell contains a given character/substring?

It would mean you can apply textual functions like Left/Right/Mid on a conditional basis without throwing errors when delimiting characters are absent.

0

11 Answers 11

412

Try using this:

=ISNUMBER(SEARCH("Some Text", A3))

This will return TRUE if cell A3 contains Some Text.

11
  • 8
    Cunning! Thanks gwin003 :) I'm still a bit surprised there's not a more intuitive function for this.
    – geotheory
    Sep 4, 2013 at 15:05
  • 20
    Yeah I agree, it would be nice if there were a CONTAINS("Text", cell) function.
    – gwin003
    Sep 4, 2013 at 15:25
  • 21
    might be worth it to note that this i case insensitive, and if you want to match case, you should use FIND() in place of SEARCH() Sep 11, 2014 at 15:19
  • 7
    got an error using , instead of ;. After changing the provided formula to =ISNUMBER(SEARCH("Some Text"; A3)) it worked. Thanks!
    – renatov
    Apr 22, 2015 at 19:18
  • 7
    @renatov that actually depends on the locale of your OS; specifically, the character used for "list separator".
    – pepoluan
    Jul 9, 2015 at 7:38
27

The following formula determines if the text "CHECK" appears in cell C10. If it does not, the result is blank. If it does, the result is the work "CHECK".

=IF(ISERROR(FIND("CHECK",C10,1)),"","CHECK")
1
  • You can use 4 spaces or tab at beginning of the line to highlight the code block. Jan 11, 2015 at 2:23
17

For those who would like to do this using a single function inside the IF statement, I use

=IF(COUNTIF(A1,"*TEXT*"),TrueValue,FalseValue)

to see if the substring TEXT is in cell A1

[NOTE: TEXT needs to have asterisks around it]

1
  • It works, but use of COUNTIF formula when huge data, makes file not responding, even file size becomes huge
    – Gaurravs
    Jun 14, 2018 at 5:42
14

This formula seems more intuitive to me:

=SUBSTITUTE(A1,"SomeText","") <> A1

this returns TRUE if "SomeText" is contained within A1.

The IsNumber/Search and IsError/Find formulas mentioned in the other answers certainly do work, but I always find myself needing to look at the help or experimenting in Excel too often with those ones.

9

Check out the FIND() function in Excel.

Syntax:

FIND( substring, string, [start_position])

Returns #VALUE! if it doesn't find the substring.

1
  • 1
    Yes nested in ISNUMBER this also works, for case-sensitive matches only.
    – geotheory
    Mar 7, 2016 at 17:39
4

It's an old question but I think it is still valid.

Since there is no CONTAINS function, why not declare it in VBA? The code below uses the VBA Instr function, which looks for a substring in a string. It returns 0 when the string is not found.

Public Function CONTAINS(TextString As String, SubString As String) As Integer
    CONTAINS = InStr(1, TextString, SubString)
End Function
1
  • 1
    Good way to improve personal efficiency, but at the cost of reproducibility I'd say :)
    – geotheory
    Jun 17, 2020 at 11:19
3

Why not simply

COUNTIF(A1,"*xyz*")

This searches for any appearence of "xyz" in cell A1.

It returns "1" when found, and "0" when not found.

Attention, the search is not case sensitive, so any of xyz, XYZ, XyZ, and so on will be found. It finds this as substrings in the cell, so also for abcxYz you get a hit.

If you do not want to write your search string into the formula itself, you can use

COUNTIF(A1,"*" & B1 & "*")

and enter your search string into B1. - Attention, when B1 is empty, the formula will return "found" ("1") as the search string is then read as "**".

2

I like Rink.Attendant.6 answer. I actually want to check for multiple strings and did it this way:

First the situation: Names that can be home builders or community names and I need to bucket the builders as one group. To do this I am looking for the word "builder" or "construction", etc. So -

=IF(OR(COUNTIF(A1,"*builder*"),COUNTIF(A1,"*builder*")),"Builder","Community")
3
  • Welcome to SO. You should read up on what makes a good answer.
    – geotheory
    Mar 23, 2016 at 13:41
  • I don't understand what this is meant to do.. it checks for builder twice when presumably once would suffice. If it finds builder it returns builder, else it returns community. The word construction doesn't appear anywhere. Perhaps something like =OR(COUNTIF(A1,"*builder*"),COUNTIF(A1,"*construction*"))? Nov 19, 2016 at 3:04
  • There is COUNTIFS: COUNTIFS(A1,"*builder*",A1,"*construction*")
    – vstepaniuk
    Jun 13, 2020 at 15:11
2

This is an old question but a solution for those using Excel 2016 or newer is you can remove the need for nested if structures by using the new IFS( condition1, return1 [,condition2, return2] ...) conditional.

I have formatted it to make it visually clearer on how to use it for the case of this question:

=IFS(
ISERROR(SEARCH("String1",A1))=FALSE,"Something1",
ISERROR(SEARCH("String2",A1))=FALSE,"Something2",
ISERROR(SEARCH("String3",A1))=FALSE,"Something3"
)

Since SEARCH returns an error if a string is not found I wrapped it with an ISERROR(...)=FALSE to check for truth and then return the value wanted. It would be great if SEARCH returned 0 instead of an error for readability, but thats just how it works unfortunately.

Another note of importance is that IFS will return the match that it finds first and thus ordering is important. For example if my strings were Surf, Surfing, Surfs as String1,String2,String3 above and my cells string was Surfing it would match on the first term instead of the second because of the substring being Surf. Thus common denominators need to be last in the list. My IFS would need to be ordered Surfing, Surfs, Surf to work correctly (swapping Surfing and Surfs would also work in this simple example), but Surf would need to be last.

2
  • most will want if/else ability, in other words a default value. Adding this quote might broaden the appeal of your answer: "There is no way to set a default if all tests return FALSE (i.e. a value if false). Instead, enter TRUE for the last test, and then a value to return as a default value if FALSE" Feb 18, 2019 at 5:25
  • IFS was added in Excel 2019, it is not present in Excel 2016.
    – robartsd
    Dec 14, 2022 at 23:11
0

Interesting *

=COUNT(MATCH("*SomeText*",A1,))
=COUNTA(VLOOKUP("*SomeText*",A1,1,))
=COUNTA(HLOOKUP("*SomeText*",A1,1,))

this returns 1 if "SomeText" is contained within A1.

1
  • You could add >0 on the end to return a boolean.
    – BigBen
    Mar 17, 2021 at 23:55
-3

Here is the formula I'm using

=IF( ISNUMBER(FIND(".",A1)), LEN(A1) - FIND(".",A1), 0 )

2
  • This checks to see if "." is included in A1, and if it is, it returns... the number of characters remaining in A1, starting with ".". Not sure whether that additional calculation is relevant here. Oct 15, 2015 at 14:20
  • Leastwise, use IFERROR(LEN(A1) - FIND(".", A1), 0) Jun 21, 2018 at 17:17

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