Concatenate Values from a Range based on a Lookup - excel

I'm working on a budget for a project with multiple phases. There is a possibility that not all phases will be worked on so I've added some lookups and SUMIF formulas so that I can get a summary of my included and excluded effort and dollar amounts. That all works fine. Now I'd like to hide my row of lookups (row 1), but still have a way of identifying which phases of the project are included and which are excluded. Obviously I could manually concatenate them together, but if the phases being included/excluded change then I need to remember to update those formulas (and it's not nearly as fun as doing all in a formula). Here's how my sheet looks:
The TEXTJOIN function seems like it should work (i.e. =TEXTJOIN(CHAR(10), TRUE, C2:N2)), but I can't wrap my head around how to make the range parameter dependent on my lookup row. I played around with INDEX using something like =TEXTJOIN(CHAR(10), TRUE, INDEX(A2:M2,,(A1:M1="Yes")*COLUMN(A1:M1))), but didn't have any luck. At the end of the day I want to have something like:
Phase 1 Phase 2 Phase 5
Please note that the above data should all appear in the same cell - using the line feed character, CHAR(10), as the delimiter in the TEXTJOIN function will make all of the phases appear on a new line within a single cell. I do not want to fill formulas through multiple cells. Thanks in advance for any help.

Please take a look at the picture. I had a similar issue in the past (and brought it to StackOverflow, at which point I was helped by #ScottCraner, original post here:
Doing an array formula lookup
Basically,
1) You set up the array you are looking through - in my case, its $A$2:$A$6, in your case it will be $B$2:$M$2.
2) You then nest the COUNTIFS function inside of a match function. The function does the following:
A) It checks whether the name of phase X has already shown up in column E when you are copy/pasting down
B) If it has, move on to the next one
C) If it hasn't, output
3) Of note: this is an array formula, so the formula itself is checking through every cell. So it looks at cell A2; if the cell in B2 is "Yes", and "Phase 1" isn't in range in column E, then put in phase 1. Because it is, its there. Then it looks at cell A3; if the cell in B3 is "Yes", the same thing for phase 2. Then it looks at cell A4; because B4 is "No", the *(B2:B6="Yes") will throw an error. and so on
4) Place error catching brackets around the function.

A less elegant way to go about this, which doesn't require array formulas would be to use the secondary column's "Yes/No" as an index. This assumes the value in the secondary column is redundant- and repeating the primary column's "Yes" or "No."
In C1: =CONCATENATE(B1,COUNTIFS($A$1:B1,B1)) -
Repeat relatively for E1, G1, etc.
Somewhere separate (the below formula assumes Row 1 of the same worksheet), you could then use: =IFERROR(INDEX($2:$2,MATCH(CONCATENATE("Yes",ROW()),$1:$1,0)-1),"")
To look up "Yes1", returning "Phase 1" and autofill downward. IFERROR is used here to return blank when you run out of "Yes" results.

Related

Need to count contents of cells to produce a knitting pattern

So this needs a bit of detail:
n,X,X,X,n is in cells B5 to F5
I need to get the following output:
1n,3x,1n
for this particular row.
Now the n's and X's represent stitches in knitting with the "n" being the background color and the "x" being the front color.
There is an array of cells B5:F12 representing the rows and stitches, so each row will have a different arrangement of stitches or background color.
I need to avoid vba as this needs to be as stable as possible with the user being my Mum who is 90 years old :) and all she needs is a place to enter the name and the layout (which I have done) and a pattern list for each row (also sorted).
I have started to consider things like:
if(B5=C5,1&B5,"")
But given the n umber of combinations that becomes very long.
Any ideas? Cheers.
You could try:
Formula in H5:
=BYROW(B5:F12,LAMBDA(x,LET(z,REDUCE(VSTACK(TAKE(x,,1),1),DROP(x,,1),LAMBDA(a,b,IF(b=#TAKE(a,,-1),IF(COLUMNS(a)=1,VSTACK(b,TAKE(a,-1)+1),HSTACK(DROP(a,,-1),VSTACK(b,DROP(TAKE(a,,-1),1)+1))),HSTACK(a,VSTACK(b,1))))),TEXTJOIN(",",,DROP(z,1)&TAKE(z,1)))))
I'll see if I can golf the bytecount down a bit...
EDIT:
After a considerable amount of golfing (came down to 119), I came up with:
=BYROW(B5:F12,LAMBDA(x,MID(REDUCE("",x,LAMBDA(a,b,IF(RIGHT(a)=b,LEFT(a,LEN(a)-2)&1+LEFT(RIGHT(a,2)),a&",1")&b)),2,99)))
Though less dynamic than the 1st one, but possible due to the fact there are only <10 columns for each knitting pattern.
If your mother doesn't have the latest Excel (with LAMBDA etc), here is an alternative to #JvdV's answer which only uses LET,SEQUENCE and FILTER.
It only accepts a single row, so you'd need to fill the formula down.
=LET(p,LOWER(B5:F5),c,COLUMNS(p),s,SEQUENCE(,c),
a,IF(s=c,c,IF(INDEX(p,,s)<>INDEX(p,s+1),s,0)),
b,FILTER(a,a>0),t,SEQUENCE(,COLUMNS(b)),
n,IF(t=1,INDEX(b,,t),INDEX(b,,t)-INDEX(b,,t-1)),
TEXTJOIN(",",TRUE,n & INDEX(p,,b)))
I might add that it allows for adding more than one colour into the pattern ...
and with a bit of conditional formatting, the good lady can design her own multicolour patterns!
This is just a start of a solution, but in cell "B6" you can put the formula:
=(IF(B5=A5,A6+1,1))
This will generate following list:
B C D E F
5: n x x x n
6: 1 1 2 3 1
From there, you can try to get the Subtotals feature to work, based on the Max formula, ... (as I said, this is just a start).
If you are willing to spread the logic over multiple sheets, it's quite easy to come up with a way to do this. Consider a workbook with three sheets:
Pattern
EqualPrevCol, where each cell of Pattern is checked for equality against the previous column of the same row.
The formula for cell EqualPrevCol!D3 is:
=Pattern!D3=Pattern!C3
And finally PatternResult, where most of the logic resides:
Consider one row of EqualPrevCol:
At every FALSE column, we want to know how many columns until the next FALSE. To do this, we want to find the next exact MATCH for D3 in the rest of the row:
=MATCH(EqualPrevCol!D3, EqualPrevCol!E3:$H3, 0)
If no match is found, that means the rest of the row is all TRUE. In this situation, we want to return the length of the rest of the row plus this current cell.
=IFNA(MATCH(...), COLUMNS(D3:$H3))
And finally, we append this to the current character:
=IFNA(...) & Pattern!D3
Also, if the 7 row at this column is TRUE, we want to keep this blank:
=IF(EqualPrevCol!D3, "", IFNA(...) & ...)
The full formula of cell PatternResult!D3 is:
=IF(EqualPrevCol!D3, "", IFNA(MATCH(EqualPrevCol!D3, EqualPrevCol!E3:$H3, 0), COLUMNS(D3:$H3)) & Pattern!D3)
Finally, the pattern is condensed to the Pattern sheet. The Pattern!B3 cell contains:
=TEXTJOIN(", ", TRUE, PatternResult!D3:$H3)
To scale this up, you simply need to change all occurrences of $H in the formulas (this was a reference to the last column) and re-fill the cells on the latter two sheets.

How to combine: INDEX + MATCH + ?VLOOKUP?

I'm having an issue with INDEX + MATCH combination:
=INDEX(ALL!$C$1:$I$1,MATCH(TRUE,ALL!C2:I2<>0,0))
At the moment the aforementioned formula does this job to an extent, where if it finds <>0 value in a row it will return header from this specific column. The issue is that the ROW (as above C2:I2) needs to be specified.
I need to vlookup values in the column "A" in sheet "ALL" and based on that, look at corresponding rows between C:I and if the value in that specific row is <>0 then return heading value.
So, in green I would need a formula to pick up numbers from "Data Source" headings, based on value 1 or any value <>0. I'm guessing it all leads somehow to some sort of "vlookup" hybrid.
Any ideas how to combine vlookup in it?
Thanks
If there can only be one '1' per row, I was thinking of this
=SUMIF(INDEX(B:E,MATCH(G2,A:A,0),0),">0",$B$1:$E$1)
Otherwise if there can be more than one '1'
=INDEX($B$1:$E$1,MATCH(TRUE,INDEX(B:E,MATCH(G2,A:A,0),0)>0,0))
to match the first value greater than zero, in this case entered as an array formula.
A simple =SUMIF() formula will do, no other convoluted INDEX() and MATCH() nested formulas required.
Let's presume we have a data-table that starts at B2 and end at
F6, like this:
So now, to comprehend the solution, here's the syntax of SUMIF() formula (Function):
=SUMIF( range, criteria, [sum_range] )
So, what we want to do is:
go over the range of C3:F3 (and each other respective row)
the criteria to when to sum, is when this range contains 1
and we want to sum_range (sum up) fixed array of numbers, so $C$2:$F$2
So the result is (for row 3):
=SUMIF(C3:F3,1,$C$2:$F$2)
and we drag the formula down, producing expected result:
PS: I think this illustrates the point very well, as to why it's important to declare not only what your formula is doing but also, what you're trying to as in whole as there often is a better (easier) way to implement something, that you might not have thought of.
In other words, follow the Minimal, Complete and Verifiable Example

Excel Formula with IF... ELSE

Hi all,
I have this excel where by I need to find the location of the item if they are found in column B.
So In my F column, I tried to write ifelse formula which didnt work.which is
=IF(D2="NULL","NONE",C((D2))).
My idea is if D2 is not null, use the value in D column to find the location in C column. In this example, fish no 4, so it is found, my F column should show the value "C" using the value shown in D column and use it as Row no in C column
I hope you guys get the idea and help me out a newbie in excel. Thanks in advance
=vlookup($D2,$A$2:$C$6,3,0)
you can use that in column F. Place that formula in F2 and copy down.
you could technically use it in column E as well, but you would need to change the 3 to a 2.
you did not say what you wanted to do if the D value was "Null" so I am going to take a stab at the dark and wrap you lookup formula in an if statement that will deal with "Null" or empty cells
=IF(OR($D2="NULL",$D2=""),"",VLOOKUP($D2,$A$2:$C$6,3,0))
That is the alternative formula to place in F2 and copy down.
Use the formula:
=IF(D2<>"NULL",VLOOKUP(D2,A2:C6,3,FALSE),"Value is NULL")
Here is the working example:
Put formula in cell F2 and drag it down.
[edit]to pull proper location column, not just the row #[/edit]
Seems like a job for MATCH+OFFSET
Try this formula in cell F2:
=OFFSET($C$1, MATCH(E2,B:B,0)-1, 0, 1, 1)
Match is used to locate the value in the first argument (ie E2) within the range specified in 2nd argument (ie B:B). I use B:B but you could also use range B2:B30 or whatever more specific range you want. (I prefer the more generic B:B, though :) )
Third paramter "0" just indicates "Exact match".
This function will retun "#N/A" if nothing found.
OFFSET takes the result from MATCH to pick out the Location you want. The first parameter in OFFSET is the rows below (or above if negative) from the base row (in this case $C$1). the next is the column: 0 since we're in the column we want to be in. The last two are the size of the range: 1,1 is a 1x1 cell, so just 1 cell. If we did ...,2,3), that would be 2 rows high and 3 columns wide - or a 6 cell range. We're just after 1 cell here.
I've always preferred MATCH + OFFSET to other options, I just found they held up more robustly to changes in a sheet (ie new rows/columns added). So it's mostly personaly preference over VLOOKUP and INDEX. I honestly have never compared their actual performance, however, I've never had any issues with MATCH+OFFSET running slowly :)

Is there a 2 Value Look up function in MS Excel that can perform the following?

I am going crazy over this. It seems so simple yet I can't figure this out. I have two worksheets. First worksheet is my data. Second is like an answer key. Upon checking checking, A1:B1 in Sheet 1 is a match with the conditions in Row 52 in SHEET 2, therefore, the value in Column C is "MGC". What is the formula that will perform this function? It's really hard to explain without the data so I pasted a link of the sample spreadsheet. Thank you so much in advance.
sample spreadsheet here. https://docs.google.com/spreadsheets/d/1_AjuNfCdGfEM-XkqPa6W4hSIxQg4NM2Vg4c2C1pQ_vQ/edit?usp=sharing
screenshot here. (wont let me post i have no reputation)
In Sheet2, insert a column in front of Column A and put the formula in A2 =C2&D2.
Then in Sheet1, Cell C2 the formula =vlookup(A2&B2,Sheet2!A:B,2,0).
the first make a concatenated key to lookup, then the second looks up that key.
How about a index(match())? If I've understood correctly you need to match across both the A and B column in sheet one, checking for the relevant values in B and C on sheet 2 to retrun worksheet 2 column a to worksheet 1 column c.
third version try:
=INDEX(Sheet2!$C$1:$C$360,MATCH(Sheet1!A1&Sheet1!B1,Sheet2!$B$1:$B$360&Sheet2!$C$1:$C$360,0))
Basically what this does is use concatenation, the & operator, to specify you are looking for "Criteria A" & "Criteria B" in sheet 1, which makes the string "Criteria A Criteria B", which is supplied in the first part of the match function.
In the second it then says match this against all of my variables in sheet 2 in the same way with concantenation.
The final part of match function (0) specifies you want an 'exact' match
It then supplied this as a reference to the index function, which then finds the row intersecting with the value you want, and returns that.
As noted here https://support.microsoft.com/en-us/kb/59482 this is an array formula, so it behaves differently, and must be input differently. https://support.office.com/en-za/article/Guidelines-and-examples-of-array-formulas-7d94a64e-3ff3-4686-9372-ecfd5caa57c7
There are (at least) 2 ways you could do this without VBA.
USING A SORTED LIST
The first relies on the assumption that your data can be re-sorted, so that everything "Unreported" is in the top, and everything "reported" is together below that (or vice versa). Assuming that this is the case (and it appears to already be sorted like this),we will use the function OFFSET to create a new range which shows only the values that align with either being "Unreported" or "Reported".
Offset takes a given reference to a point on a sheet, and then moves down/up & left/right to see what reference you want to return. Then, it returns a range of cells of a given height, and a given width. Here, we will want to start on Sheet2 at the top left, moving down until we find the term "Unreported" or "Reported". Once that term is found, we will want to move one column to the right (to pull column B from sheet 2), and then have a 'height' of as many rows as there are "unreported" or "reported" cells. This will look as follows in A1 on sheet 1, copied down:
=OFFSET(Sheet2!$A$1,MATCH(A1,Sheet2!A:A,0)-1,1,COUNTIF(Sheet2!A:A,A1),1)
This says: First, start at cell A1 on sheet2. Then find the term in A1 (either "unreported" or "reported", on sheet2!A:A (we subtract 1 because OFFSET starts at A1 - so if your data starts at A1 we need to actually stay at "0". If you have headers on sheet2, you will not need this -1). Then, move 1 column to the right. Go down the rows for as many times as Sheet2 column A has the term found in Sheet1 A1. Stay 1 column wide. Together, this will leave you with a single range on sheet2, showing column B for the entire length that column A matches your term in sheet1 A1.
Now we need to take that OFFSET, and use it to find out when the term in Sheet1 B1 is matched in Sheet2 column B. This will work as follows:
=MATCH(B1,[FORMULA ABOVE],0)
This shows the number of rows down, starting at the special OFFSET array created above, that the term from B1 is matched in column B from sheet2. To use this information to pull the result from column C on sheet 2, we can use the INDEX function, like so:
=INDEX([FORMULA ABOVE],MATCH(B1,[FORMULA ABOVE],0))
Because this would be fairly convoluted to have in a single cell, we can simplify this by using VLOOKUP, which will only require the OFFSET function to be entered a single time. This will work as follows:
=VLOOKUP(B1,[FORMULA ABOVE],2,0)
This takes the OFFSET formula above, finds the matching term in B1, and moves to the 2nd column to get the value from column C in sheet2. Because we are going to use VLOOKUP, the offset formula above will need to be adjusted to provide 2 columns of data instead of 1. Together, this will look as follows:
FINAL FORMULA FOR SHEET1, C1 & COPIED DOWN
=VLOOKUP(B1,OFFSET(Sheet2!$A$1,MATCH(A1,Sheet2!A:A,0)-1,1,COUNTIF(Sheet2!A:A,A1),2),2,0)
OPTION USING ARRAY FORMULAS
The above method will only work if your data is sorted so that the REPORTED and UNREPORTED rows are grouped together. If they cannot be sorted, you can use an ARRAY FORMULA, which essentially takes a formula which would normal apply to a single cell, and runs it over an entire range of cells. It returns an array of results, which must be reduced down to a single value. A basic array formula looks like this [assume for this example that A1 = 1, A2 = 2...A5 = 5]:
=IF(A1:A5>3,A1:A5,"")
Confirm this (and all array functions) by pressing CTRL + SHIFT + ENTER, instead of just ENTER. This looks at each cell from A1:A5, and if the value is bigger than 3, it gives the number from that cell - otherwise, it returns "". In this case, the result would be the array {"";"";"";4;5}. To get the single total of 9, wrap that in a SUM function:
=SUM(IF(A1:A5>3,A1:A5,""))
In your case, we will want to use an array formula to see what row in Sheet2 matches A1 from Sheet1, and B1 from Sheet1. This will look like this:
=IF(Sheet2!$A$1:A$100=A1,IF(Sheet2!$B$1:$B$100,ROW($B$1:$B$100),""),"")
This checks which rows in column A from sheet 2 match A1. For those that do, it then checks which rows in column B from sheet 2 match B1. For those, it pulls the row number from that match. Everything else returns "". Assuming no duplicates, there should only 1 row number which gets returned. To pull that number from the array of results, wrap the whole thing in a MATCH function. Now that you have the row number, you can use an INDEX function to pull the result in Column C with that row, like this:
FINAL ARRAY FORMULA METHOD
=INDEX($C$1:$C$100,MAX(IF(Sheet2!$A$1:A$100=A1,IF(Sheet2!$B$1:$B$100,ROW(Sheet2!$B$1:$B$100),""),"")))
Remember to confirm with CTRL + SHIFT + ENTER instead of just ENTER, when you type this formula. Note that I didn't refer to all of Sheet2!A:A, because array formulas run very slowly over large ranges.
The following formula should work without making any changes to the datasheets.
=INDEX(Sheet2!$A$1:$A$360,MATCH(Sheet1!A1,IF(Sheet2!$C$1:$C$360=Sheet1!B1,Sheet2!$B$1:$B$360),0))
Remember to save this formula as an array with CTRL+SHIFT+ENTER
Documentation on how to use INDEX and MATCH against multiple criteria can be found on Microsoft Support.
It's not clear what you want to do with the multiples that do not have corresponding matches. txed is listed as Unreported twice in Sheet1; kntyctap is listed as Unreported three times. There are only one corresponding match on Sheet2 for each of these.
Non-array Standard Formulas for multiple criteria matches
For Excel 2010 and above use this standard formula in Sheet1!C1:
=IFERROR(INDEX(Sheet2!$A$1:$A$999,AGGREGATE(15,6,ROW(1:999)/((Sheet2!$B$1:$B$999=A2)*(Sheet2!$C$1:$C$999=B1)), COUNTIFS(A$1:A1, A1, B$1:B1, B1))), "")
For version of Excel prior to 2010 use this standard formula in Sheet1!C1:
=IFERROR(INDEX(Sheet2!$A$1:$A$999, SMALL(INDEX(ROW($1:$999)+((Sheet2!$B$1:$B$999<>A1)+(Sheet2!$C$1:$C$999<>B1))*1E+99, , ), COUNTIFS(A$1:A1, A1, B$1:B1, B1))), "")
I've handled error with the IFERROR function in that latter formula. Excel 2003 and previous may have to use an IF(ISERROR(..., ...)) combination.

Sum cells values with a separation of 3 columns in Excel

I would like to create a formula that sums all the values that I'm going to add in the future in some cells of the same row. For example, I would like to add cells D3,G3,J3,M3 and so on (separated 3 rows) in cell D1.
Best regards,
A good solution will depend on what (if anything) is in the intervening columns on row 3. A SUMPRODUCT function can produce some nice results by checking the stagger or offset of the columns but it isn't going to like text put into the intervening cells.
=SUMPRODUCT((INDEX(3:3, 1, 4):INDEX(3:3, 1, MATCH(1E+99,3:3 )))*NOT(MOD(COLUMN(INDEX(3:3, 1, 4):INDEX(3:3, 1, MATCH(1E+99,3:3 )))-1, 3)))
By switching to an array formula and a condition SUM function, you should be able to happily skip over any text values.
=SUM(IF(NOT(MOD(COLUMN(INDEX(3:3,1,4):INDEX(3:3,1,MATCH(E1+99,3:3)))-1,3)),INDEX(3:3,1,4):INDEX(3:3,1,MATCH(E1+99,3:3))))
Array formulas need to be finalized with Ctrl+Shift+Enter↵.
If neither ot those fits the bill, provide a little more information on the nature of what not to sum and more help can be offered.
This will do the job:
1) put =SUM(D2:XFD2) in cell D1
2) put =IF(MOD(COLUMN(D3)+2,3)=0,IF(D3="","",D3),"") in all cells from (D2 .. til the end)
3) put your data in D3 .... til the end.
Anonymous' solution works if you only need to sum one row. If you need to be able to drag that formula down, consider using a flag in row 2 to identify every 3rd column (like Anonymous did) , and use a sumif formula to add the columns where that flag is present.
Type
=
in the cell where your first result will appear (e.g. D1).
Click on the first value in the first row.
Press + on numpad.
Click on the next value in the next row.
Repeat steps 4-5 until you reach the last row.
Press Enter.
You can now select the results-cell (e.g. D1) and click and drag the small button to your lower right, across all the columns.
If you have a static reference - a constant in a cell that you do not wish to move use the $ operator, for example: $C$3 will not move column-wise or row-wise. $C3 can move column-wise but not row-wise and the opposite is true for C$3. This can be achieved in the Formula Bar, where you can also manually type in the function.

Resources