+ Reply to Thread
Results 1 to 6 of 6

Replacing a bunch of nested IFs

  1. #1
    Registered User
    Join Date
    04-16-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    3

    Replacing a bunch of nested IFs

    Hi everyone.

    I have a list of sentences in a column.
    I need to check for particular strings within those sentences.
    Depending on the string I find in each sentence (each sentence only has one of the strings I'm searching, I just don't know which) I want the cell with the formula to return a given second string.

    With an example it's easier to explain:

    Sentences
    ========
    The quick brown fox jumps over the lazy dog
    To be or not to be that is the question
    Are you talking to me?

    Strings to be searched within the sentences | Second strings to be returned if the the string is found within the sentences
    (different strings might return the same value, although not on this example)
    ====================================================================
    brown | Test sentence
    question | Shakespeare
    talking | Taxi Driver

    I'm using, and it works, nested IFs. But it's far from elegant or easy to maintain:

    =IF(ISNUMBER(FIND("brown";A1));"Test sentence";IF(ISNUMBER(FIND("question";A1));"Shakespeare";... and so on


    How can I make this more elegant and using a list of strings to be searched + corresponding second strings to be returned?


    Thank you in advance for you time.

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,412

    Re: Replacing a bunch of nested IFs

    Suppose you put those words in F1, F2 and F3, and the strings to be returned in G1:G3, then you could have this formula in B1:

    =INDEX(G:G,SUMPRODUCT(--(ISNUMBER(SEARCH(F$1:F$3,A1))),ROW(F$1:F$3)))

    and copy this down. You should extend the range as shown in red if you have more words to search for. If none of the words are found then the formula will return zero.

    Hope this helps.

    Pete

  3. #3
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,412

    Re: Replacing a bunch of nested IFs

    If you want to trap the zero for no matched words and return a message instead, then you can do this:

    =IFERROR(INDEX(G$1:G$3,SUMPRODUCT(--(ISNUMBER(SEARCH(F$1:F$3,A1))),ROW(F$1:F$3))),"not present")

    Again, change the ranges in red to suit your list.

    Hope this helps.

    Pete

  4. #4
    Registered User
    Join Date
    04-16-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Replacing a bunch of nested IFs

    Thank you very much, Pete. In times like this I realize how little I know about Excel and its functions.

    It worked. I just have to troubleshoot now why in _some_ rows I get the wrong result.

    FWIW, we're talking about 500 lines with several types of sentences and I'm searching for 75 different strings to display 45 different resulting strings.

    The purpose of it to classify bank account lines based on the description of each line (meal, supermaket, mortgage, insurance etc.).

  5. #5
    Registered User
    Join Date
    04-16-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Replacing a bunch of nested IFs

    I found the problem. What is happening is that

    =SUMPRODUCT(--(ISNUMBER(SEARCH(F$1:F$3,A1))),ROW(F$1:F$3))

    is returning the sum of all rows indexes which include a matching string whereas a have to account for only the first one.

    Can you help me, please? I've tried a few methods but I don't seem to be getting there.

    Thank you.

  6. #6
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,412

    Re: Replacing a bunch of nested IFs

    You said in your first post:

    each sentence only has one of the strings I'm searching
    which is why I was able to suggest that formula. However, if you have several matches then it won't work correctly, as you have found out.

    Post an example workbook so I can see more of the variations that you have encountered.

    Pete

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looking Up a Value From a Bunch of Data
    By Howdoesitwork in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-17-2014, 05:23 AM
  2. Looking forward to learn a bunch...
    By floridahockeyguy in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 07-24-2013, 09:15 AM
  3. [SOLVED] replacing nested if statements with some kind of VBA loop?
    By cwyn in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-27-2012, 01:46 PM
  4. Graph a BUNCH of Categories
    By hutch@edge.net in forum Excel Charting & Pivots
    Replies: 14
    Last Post: 02-11-2009, 09:45 AM
  5. Correlation Matrix:bunch of nested if statements
    By bs272 in forum Excel General
    Replies: 1
    Last Post: 09-02-2008, 02:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1