Search code examples
excelarray-formulas

Return values, based on value of an intersection of a row and column


I want to return a label(s) based on an intersection of a Row and Column equal to "Yes".

            |       Location       |    
ID | Tool   | Wall | Bin | Toolbox | Count   
---+--------+------+-----+---------+-------
1. | Axe    | YES  |     |         | 1  
2. | Hammer |      |     | YES     | 5      
3. | Pliers |      |     | YES     | 2      
4. | Nails  |      | YES |         | 500        
5. | Hoe    | YES  |     |         | 2  
6. | Screws |      | YES |         | 200    
7. | Saw    | YES  |     |         | 3

What's in Toolbox? (Results wanted)

Axe,Wall, 1 
Hammer, Toolbox, 5
Pliers,Toolbox, 2
Nails,Bin, 500
Hoe, Wall, 2
Screws, Bin, 200
Saw, Wall, 3

I also want to be able add Tools and Locations?


Solution

  • Without using VBA, this is going to be a bit of a pain, but workable if you don't mind helper columns. I don't advise trying to do this in a single Array Formula, because text strings are hard to work with in Array formulas. That is - if you have an array of numbers, you can turn that into a single result a lot of ways (MIN, MAX, AVERAGE, SUM, etc.). However with an array of strings, it's harder to work with. Particularly, there's no easy way to concatenate an array of strings.

    So instead, use a helper column. Assume column A = toolname, column B = a check for being on the wall, column C = a check for being in the bin, column D for being in the toolbox, and column E for the number available.

    FORMATTING SIDE NOTE

    First, I will say that I recommend you use TRUE/FALSE instead of "yes"/"no". This is because it is easier to use TRUE / FALSE within Excel formulas. For example, if you want to add A1 + A2 when A3 = "yes", you can do so like this:

    =IF(A3="yes",A1+A2)
    

    But if you want to check whether A3 = TRUE, this is simplified:

    =IF(A3,A1+A2)
    

    Here, we didn't need to hardcode "yes", because A3 itself will either be TRUE or FALSE. Also consider if you want to make a cell "yes" if A3 > 5, and otherwise be "no". You could do it like this:

    =IF(A3>5,"yes","no)
    

    Or, if you used TRUE/FALSE, you could simply use:

    =A3>5
    

    However, I'll assume that you keep the formatting you currently have (I would also recommend you just have a single cell that says either "toolbox"/"bin" etc., instead of 4 columns where 1 says "yes", but we'll also assume that this needs to be this way).

    BACK TO YOUR QUESTION

    Put this in column F, in F2 for the first cell:

    =Concatenate(A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)
    

    Concatenate combines strings of text into a new single text string. You could also use &; like: A2 & " " etc., but with this many terms, this is likely easier to read. Index looks at your header row 1, and returns the item from the first column which matches "yes" in the current row.

    Then put the following in F3 and drag down:

    =Concatenate(F2," ", A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)
    

    This puts a space in between the line above and the current line. If instead you want to make each row appear after a line-break, use this:

    =Concatenate(F2,CHAR(10), A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)