+ Reply to Thread
Results 1 to 14 of 14

Searching for unknown text string in a range and then matching it to a reference table

  1. #1
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Question Searching for unknown text string in a range and then matching it to a reference table

    Hi everyone,

    First time poster. I am hoping you folks can kindly help me with a very complicated set of questions. I have been trying to look this up for the last two days and I cannot seem to figure out a way to do it.

    This post has been revised due to breadth of the question.

    Essentially - I want to be able to have a function that searches the third row in column A for a text string because only the suspected diagnosis will be filled in, and the other cells will be blank. The problem is that I do not know what string I am searching for! It could be A, B, C, or A and B, etc. So how do I get excel to search a range of cells for to see if ISTEXT=true and then return to me what the value of the text was so I know what I am dealing with.

    Please see below for a revised question. Find my excel sample worksheet attached Colors.xlsx. Thank you!
    Last edited by the_nibs; 06-05-2015 at 05:00 PM. Reason: revising the post - not succinct enough

  2. #2
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    I hope it's okay to bump this. I don't need a strict solution, I just want to know if it is possible to do these things and what functions to use..

    If nothing else, I would appreciate it if someone could help with the first part. Is there a way to search a range say A1:A5 to find all the cells that have a text value (the others will be blank) and to have it return the value of the cells that are text? i.e. if I have the following cells: <red> <blank> <blue> <blank> <blank> is there a way to search those cells to give me the result 'red' 'blue'?

    Many thanks.

    -Nibs

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Quote Originally Posted by the_nibs View Post
    if I have the following cells: <red> <blank> <blue> <blank> <blank> is there a way to search those cells to give me the result 'red' 'blue'?
    Data Range
    A
    B
    C
    D
    1
    Red
    Red
    Red, Blue
    2
    Blue
    3
    Blue
    4
    5
    ------
    ------
    ------
    ------


    If you want the results in separate cells like in col C then we can use a formula to do it.

    If you want the results to be a concatenated string like in col D then you'll need to use a VBA function to do it.

    Which method do you want to use?
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Colors.xlsxIn separate cells would work. My raw data is transposed though. Sorry, I don't know how to make that table you just showed but my data shows up like this:

    A....B....C....D....E
    red..<>...<>..blue..<>
    <>..grey..<>...<>..<>
    <>..<>...green..<>..<>
    <>..grey...<>...<>..pink

    Is there a way to make it show up 'red' and 'blue' for the first row, 'grey' for the second, 'green' for the third, 'grey' and 'pink' for the fourth row, etc?

    Thanks!
    Last edited by the_nibs; 06-04-2015 at 11:54 PM. Reason: Attached file

  5. #5
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Try this...

    This formula entered in G3:

    =IFERROR(INDEX($B3:$E3,AGGREGATE(15,6,1/($B3:$E3<>"")*COLUMN($B3:$E3),COLUMNS($G3:G3))-COLUMN($B3)+1),"")

    Copy down to G9 then across until you get a column full of blanks.

  6. #6
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Anyone have a way to tackle this?

    Thanks,

    -Nibs

  7. #7
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Quote Originally Posted by Tony Valko View Post
    Try this...

    This formula entered in G3:

    =IFERROR(INDEX($B3:$E3,AGGREGATE(15,6,1/($B3:$E3<>"")*COLUMN($B3:$E3),COLUMNS($G3:G3))-COLUMN($B3)+1),"")

    Copy down to G9 then across until you get a column full of blanks.


    Thank you!!
    it seems to work. I have never heard of the aggregate function. I'm going to look this up and ask you to explain the formula if thats okay.

  8. #8
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    AGGREGATE is a new function that was introduced in Excel 2010.

  9. #9
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    I am a fairly novice excel user so please bear with me

    Step 1 - $B3:$E3 <>=""
    Finds all cells in the range that are not blank and replace with logical true/false expressions

    Step 2 - divide 1 by the boolean operators which automatically converts true and false operators into 1/0s (this step is purely to generate an error on the blank cells for the aggregate function, correct? Very clever)

    Step3 - As mentioned, 1/0 yields an error in call cases where there are blank cells

    Step 4 - I do not understand why you are multiplying this by the columns from B3-E3 - I see the result makes all the blank cells into division by zero errors

    Step 5 - I don't understand what the columns ($G3:G3) does, it seems to be part of the aggregate function

    Step 6 - the aggregate function using the small function and ignoring errors. This is returning the smallest values of the array + the columns argument which I don't get. Why do you need to use the small function? Sorry - this is probably a basic concept for the index function but I have never used that before..

    Step 6 - I don't understand the last part with the columns being subtracted and the +1?

    Step 6 - If error function returns the value of the index function on cells that are no-blank and returns a blank cell for cells that were blank to begin with (and giving errors)


    Thank you very much for taking the time to help me with this.

  10. #10
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    I have marked the thread as solved but I would still appreciate it if anyone else could also explain the formula to me.. Thank you!

  11. #11
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    I'll get back to you later this afternoon (Sun, June 7) when I have more time.

  12. #12
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Sorry about the delayed response! I got side tracked!

    Here goes...

    =IFERROR(INDEX($B3:$E3,AGGREGATE(15,6,1/($B3:$E3<>"")*COLUMN($B3:$E3),COLUMNS($G3:G3))-COLUMN($B3)+1),"")

    We're telling Excel we want to look in this range for some data:

    INDEX($B3:$E3

    Since the range is a 1 dimensional array (a single row or column) the data is located by its relative position within the range.

    B3 = position 1
    C3 = position 2
    D3 = position 3
    E3 = position 4

    We want to extract the data from the cells that are not empty. The data we want to find is defined by this criteria:

    1/($B3:$E3<>"")*COLUMN($B3:$E3)

    <>"" means "is not blank"

    This expression will return a Boolean value of either TRUE or FALSE:

    $B3:$E3<>""

    Data Range
    B
    C
    D
    E
    1
    2
    3
    Red
    Blue
    4
    ------
    ------
    ------
    ------


    B3 <>"" = TRUE
    C3 <>"" = FALSE
    D3 <>"" = TRUE
    E3 <>"" = FALSE

    This result is then used in a division operation:

    1/TRUE = 1
    1/FALSE = #DIV/0!

    B3: 1/TRUE = 1
    C3: 1/FALSE = #DIV/0!
    D3: 1/TRUE = 1
    E3: 1/FALSE = #DIV/0!

    This result is then multiplied by the absolute column numbers of the range:

    B3: 1 * COLUMN(B3) = 2
    C3: #DIV/0! * COLUMN(C3) = #DIV/0!
    D3: 1 * COLUMN(D3) = 4
    E3: #DIV/0! * COLUMN(E3) = #DIV/0!

    Now we have this array: {2,#DIV/0!,4,#DIV/0!}

    The AGGREGATE function has many applications. In this application we're using it to return the column numbers of the cells in the range B3:E3 that are not empty.

    AGGREGATE(15

    15 means return the smallest number

    The function has an option that allows it to ignore error values:

    AGGREGATE(15,6

    6 means ignore error values

    So, we can take advantage of this feature by intentionally generating errors for items that we want to ignore. In this case, cells that are empty. That's why we use the division operation in this expression:

    1/($B3:$E3<>"")*COLUMN($B3:$E3)

    Now we have the absolute column numbers of the cells that are not empty:

    {2,#DIV/0!,4,#DIV/0!}

    However, as noted above the INDEX function uses relative position numbers so we have to convert the absolute column numbers to relative position numbers. We do that with this expression:

    -COLUMN($B3)+1

    B3: 2 - COLUMN($B3) + 1 = 1
    C3: #DIV/0! - COLUMN($B3) + 1 = #DIV/0!
    D3: 4 - COLUMN($B3) + 1 = 3
    E3: #DIV/0! - COLUMN($B3) + 1 = #DIV/0!

    As we copy the formula across the row we use this expression to increment the argument to return the nth smallest position number:

    COLUMNS($G3:G3)

    If we enter the first formula in cell G3 then:

    COLUMNS($G3:G3) = 1 = return the 1st smallest position number
    COLUMNS($G3:H3) = 2 = return the 2nd smallest position number
    COLUMNS($G3:I3) = 3 = return the 3rd smallest position number
    COLUMNS($G3:J3) = 4 = return the 4th smallest position number

    These numbers are then passed to the INDEX function telling it to return the value of the nth position.

    If the formula is copied to more cells than there are cells in the data range that aren't empty then it will return an error. So we use the IFERROR function to suppress those errors and return a blank instead.

    Hopefully that makes some sense!
    Last edited by Tony Valko; 06-08-2015 at 07:20 PM.

  13. #13
    Registered User
    Join Date
    06-02-2015
    Location
    Hamilton, Canada
    MS-Off Ver
    2013
    Posts
    14

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Tony, thank you so much for taking the time to explain that. I know it probably took you a while to type that.

    I think I follow everything up until the '-COLUMN($B3)+1' - why not simply subtract 1 i.e. 1/($B3:$E3<>"")*COLUMN($B3:$E3)-1 as we know the absolute position (column b = 2) is always 1 greater than the relative position of the first cell so column b = 2-1 = 1, c = 3-1 = 2, etc.

    oh, i see. the columns($G3:G3) is acting as the constant for the k-th smallest number for the aggregate function

    super clever thanks again

  14. #14
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Searching for unknown text string in a range and then matching it to a reference table

    Quote Originally Posted by the_nibs View Post
    Tony, thank you so much for taking the time to explain that. I know it probably took you a while to type that.
    Yes, it did. I'm a one finger typer!

    I think I follow everything up until the '-COLUMN($B3)+1' - why not simply subtract 1 i.e. 1/($B3:$E3<>"")*COLUMN($B3:$E3)-1 as we know the absolute position (column b = 2) is always 1 greater than the relative position of the first cell so column b = 2-1 = 1, c = 3-1 = 2, etc.
    Not necessarily. Make the data range F5:I5 with 1/($F5:$I5<>"")*COLUMN($F5:$I5)-1

    See what results you get.

    oh, i see. the columns($G3:G3) is acting as the constant for the k-th smallest number for the aggregate function
    Yes!

+ 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. Replies: 2
    Last Post: 07-31-2013, 05:57 PM
  2. Searching for text in a string
    By Jedski in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-10-2012, 03:44 PM
  3. [SOLVED] Searching for a text string within a range of cells
    By mxplut in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-27-2012, 12:00 PM
  4. [SOLVED] Searching a text string in a range of cells.
    By heenanmc in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-15-2006, 12:55 PM
  5. Searching a String for a Matching Word from another String
    By ExcelTip in forum Tips and Tutorials
    Replies: 0
    Last Post: 08-23-2005, 04:01 AM

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