Forum Discussion

moncho47's avatar
moncho47
Copper Contributor
Jun 06, 2020

Excel formula - returning multiple text strings in a cell

Hi, Newbie here. Is have a cell that could contain apples and/or oranges and/or pears. I'm able to use an IF formula to extract either apples or oranges or pears but get a false value when the cell contains 'apples and oranges'. Is there a way to extract such a combination out of a cell?

  • moncho47 

    As variant if add list of possible variant in column O, and assuming your version of Excel supports TEXTJOIN,

    in M2 is

    =IF(
       LEN(TEXTJOIN(", ",1,IF(COUNTIF(B2,"*"&$O$2:$O$4&"*"),$O$2:$O$4,"")))=0,
       "apple",
       TEXTJOIN(", ",1,IF(COUNTIF(B2,"*"&$O$2:$O$4&"*"),$O$2:$O$4,""))
    )

    and drag it down.

  • mathetes's avatar
    mathetes
    Silver Contributor

    moncho47 

     

    Could you post a representative spreadsheet that illustrates what you're describing. One that works on the one hand, but not on the other. It's hard to diagnose without actually seeing and experiencing the reality, no matter how clear your description is (and it's pretty good)...

     

    I do realize that your apples and oranges is meant as an example, but I'm having a hard time visualizing why you can make the one condition work, but not the other.

    • moncho47's avatar
      moncho47
      Copper Contributor

      mathetesThank you for replying. Much appreciated. I've attached an example of what I'm trying to get at. Hope it makes sense. I'm amazed at what Excel can do but not sure if what I'm hoping to do is beyond it (I doubt it!)

  • hynguyen's avatar
    hynguyen
    Iron Contributor

    moncho47 If you do not care for the exact order of occurrence of the found texts in the cell, the attached workbook may return the results you want (use the formula in the last column, assuming that you have a list of your search texts in cell D1, E1, F1). It is always better to have the search texts listed and use reference in the formula to those cells rather than hardcoded. You can always expand the list of your search texts and simply add more components to the formula.

    • moncho47's avatar
      moncho47
      Copper Contributor

      hynguyenThank you for taking the time to build this example. It looks very complicated. I'll try to see if I can use it in the example I've just attached. Much appreciated.

      • moncho47 

        As variant if add list of possible variant in column O, and assuming your version of Excel supports TEXTJOIN,

        in M2 is

        =IF(
           LEN(TEXTJOIN(", ",1,IF(COUNTIF(B2,"*"&$O$2:$O$4&"*"),$O$2:$O$4,"")))=0,
           "apple",
           TEXTJOIN(", ",1,IF(COUNTIF(B2,"*"&$O$2:$O$4&"*"),$O$2:$O$4,""))
        )

        and drag it down.

Resources