I'm working on a spreadsheet that does some pretty bizarre calculations... The long and the short of it is that I need to search the values in the 4 folowing ranges.
C5:C293, E5:E293, G5:G293 & I5:I293. The issue is that those ranges can change but will stay consistent across the columns. So For example instead of 5:293 it might change to 5:290. Is there an easy way to say "Look at the cells that are rows X:Y and are in columns A, B, C or D?"
google-spreadsheets
ARRAYFORMULA: Commas , arrange the array horizontally and semi colons; arrange the array vertically( stacking on top of each other)
=ARRAYFORMULA ({C5:C293,E5:E293,G5:G293,I5:I293})
QUERY: If there's a pattern in the columns, This will isolate the array:
=QUERY(TRANSPOSE(C5:I293),"Select * skipping 2",0)
In Excel, use the INDIRECT() function.
So, have a Start cell in A1, and an End cell in A2. Then your formulas to sum a range would reference the ranges for C column as:
=SUM(INDIRECT("C"&A1&":C"&B1))
And D column as:
=SUM(INDIRECT("D"&A1&":D"&B1))
Use range names rather than Indirect() or Offset() functions in the worksheet.
Create one range that covers the desired rows in the first column, e.g. "red" in the example, then create additional range names that offset the required columns from that range name.
How you construct your initial range is totally up to the circumstances, but the range names with Offset() will be super fast to deliver a result, whereas Indirect() and Offset() in worksheet cells can cause significant speed issues.
Use offset function, if you are in google-spreadsheets, use it dinamically:
=offset(C5,,,counta(C5:C),1)
will reproduce the range of a proper length.
Related
I think I made a breakthrough with "ultra-dynamic" ranges. That's how I call them because not only they are dynamic; but also they refer to different ranges based on the cell address in which they are written in! Now I need to advance this breakthrough even further. Those of you using dynamic ranges in Excel, especially the dynamic-range-gurus, will be thrilled to read below and can possibly help in this advancement:
Disclaimer: If you are not familiar with dynamic ranges do not attempt to read below!
Background:
Our sheet has cells with calculations on top and a pivot table below.
Each cell above refers to the pivot table cells below in the same column.
The first column of the pivot table (titled "Row Labels" by default) is sorted from top to bottom in descending order. Each of the next columns has the result of a different test.
At one point somewhere in the middle rows of the pivot table there is a "marker line" that separates the top part of the pivot from the bottom one.
Let's call the top part "Uppers" and the bottom part "Downers".
Let's call both parts together "Population". Population is a non-contiguous range because the "marker line" that separates the Uppers from the Downers intervenes.
For each cell above the pivot there are calculations for the pivot column exactly below that need to refer to the Uppers or Downers or Population of the column itself.
Previously, my formulas in all of the cells above were a repetition of something like this below:
= MAX( OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1) )
This example gives the maximum of the area of the Downers in the same column below.
Where:
A79 is the top left corner of the pivot table,
B5 has the row number of the marker line (relative to A79) to signify that our column's Downers area starts from the first row below that marker row (hence the "+1"). The formula of that cell has an XMATCH formula to produce the resulting row number. For example if the Uppers are positive numbers and the Downers are negative numbers, all sorted in descending order, the XMATCH is searching for the 0 row that separates the two parts of the population.
COLUMN()-1 returns the current column's number for offsetting to the right and the -1 is necessary because the first column is 0 rather than 1 offset to the right, and
B6 has the number of rows ("height") to the last row number of the pivot (relative to B5) where our Downers end. The cell contains the usual COUNTA function that counts the number of rows in the pivot table and from this number it subtracts the number of the marker row in B5 to get the "height" of the Downers range.
The OFFSET... part of the formula when copied to any cell with calculations above the pivot will always give the Downer's area in the same column.
And here we come to the "ultra"-dynamic part: I tested (and it works!) naming a range as:
ColDowners: =OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1)
And I substituted all the cells with calculations that refer to the Downers with something like:
= MAX( ColDowners )
Miraculously, all cells with this named ultra-dynamic range, are calculated with the correct values for the Downers exactly below them in the same column! Sometimes when the Workbook is initially loaded, the cells appear with 0 values, but immediately when you hit F9 to Calculate they get the correct value (which of course is not a problem).
So this range is "dynamic" in two ways: (1) because start and end rows are dynamic (as usual) and (2) because it results to a different range depending on where it's written!!
And now for the advancement. Any dynamic-range-guru's input would be extremely valuable:
OFFSET is a volatile function with the known performance issues. Can we replace it with two non-volatile INDEX functions separated by a colon (":")? I know how to name a range that starts from a specific cell and ends in a different one every time (eg. =A$155:INDEX(...COLUMN()...)). But can BOTH the start and end of the range be INDEXified [sic]? i.e. can it be something like =INDEX(...COLUMN()...):INDEX(...COLUMN()...). And, consequently, if we name a range with this formula, will it work?
Answers will have to exclude volatile functions like INDIRECT and will have to be as simple as possible. The resulting range of the formula of the ultra-dynamic range will have to be different depending on which COLUMN() it's written in in the sheet (like the OFFSET one above) and will have to be "able" to start from a specific number of rows below A79 (the number written in B5 in the example above) and end in the one written in B6.
The correct formula that defines an ultra-dynamic-range for replacing the 'OFFSET' formula:
=OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1)
Is:
=INDEX($A:$XX,ROW($A$79)+$B$5+1,COLUMN()):INDEX($A:$XX,ROW($A$79)+$B$7,COLUMN())
Where in B7 is the number of the last row of the pivot. (=Something like B5+B6 plus/minus 1 or 2 - test it for your case)
If you Define a Name of a Dynamic Range with this ultra-dynamic formula, it will adapt to give you different ranges depending on the location of the cell where you copied it to!! It will always give you the same parallel range in your column and will produce different results in different columns! I tested it and it works perfectly, plus it made my calculations lightning faster since INDEX is non-volatile (as opposed to OFFSET).
One more tip for the example above: I also tried nested range names and they work! For example I Defined the Range for ColPopulation as:
=(ColUppers,ColDowners)
Note that this is an ultra-dynamic-nested-range-name! Of course, it can work only for simple functions (such as =MAX(ColPopulation)) and will not work in functions such as SUMPRODUCT that need contiguous ranges. Still, it is a very useful thing to know that you can Define Ranges by adding other range names with commas!
Great help from all involved! Thanks a lot!
I'm trying to write a formula using SUMPRODUCT, SUMIF, INDIRECT and a named range. My formula is:
=SUMPRODUCT(SUMIF(INDIRECT("'"&INDIRECT("EightiesNames")&"'!G8:HI8"),$A$2,INDEX(INDIRECT("'"&INDIRECT("EightiesNames")&"'!G9:HI300"),MATCH($A$1,INDIRECT("'"&INDIRECT("EightiesNames")&"'!E9:E300"),0),0)))
The formula goes through every worksheet in the named range and does a lookup to both the columns (looking up $A$2 in G8:HI8. note that $A$2 may occur multiple times in one worksheet) and rows (looking up $A$1 to E9:E300) before summing together all the return values with SUMIF/SUMPRODUCT.
The formula was inspired by the below simplified version but with SUMPRODUCT and INDIRECT layered in to allow it to perform the same function across multiple tabs.
=SUMIF(G8:HI8, $A$2, INDEX(G9:HI300, MATCH($A$1, E9:E300,0), 0))
See below for a more detailed description of the workbook.
Worksheets:
Sheet1 - houses a named range in cells B2:B7 {Name1, Name2, Name3, Name4, Name5, Name6}. The named range is called "EightiesNames"
Name1 - where the formula is located. The formula references worksheets Name1, Name2, Name3, Name4, Name5, Name6, basically all the worksheets in the named range.
There are three occurrences of "INDIRECT("'"&INDIRECT("EightiesNames")" - after the SUMIF, one after INDEX and after the MATCH. The first occurrence of the two INDIRECTs, works the way I want it to (when I evaluate the formula). It expands the INDIRECT("EightiesNames") into an array {"Name1";"Name2";"Name3";"Name4";"Name5";"Name6"}. The formula then proceeds to tack on the range "'!G8:HI8" after each "Name" - "'Name1'!G8:HI8";...;"'Name6'!G8:HI8".
However, for the INDIRECTs that occur after the INDEX and the MATCH, it does not return an array like the first one. On my computer, it returns "Name2" (returns "Name1" on my friend's), not sure why its different and why it returns those values instead of the entire array.
I'm not really sure how to fix this as the three INDIRECT formulas are identical but do not produce the same result. I also know INDIRECT formulas take up a lot of processing power so I'll gladly take alternatives. Any help/insight would be greatly appreciated! Thanks!
While a single formula solution is possible, it would be very complex, inefficient, and not robust at all. For this reason, I would suggest that you use a regular SUMIF formula for each worksheet, where it would be placed in the same cell for each of your worksheets, let's say B1, and then you could simply use SUM to get your total, for example =SUM('Sheet1:Sheet5'!B1).
However, if you would like to try a single formula solution, try the following formula that needs to be confirmed with CONTROL+SHIFT+ENTER.
Note that I have assumed that G8:HI8 and E9:E300 for each of your worksheets contain text values. If G8:HI8 contains numerical values, replace...
T(OFFSET(INDIRECT("'"&EightiesNames&"'!G8:HI8"),,COLUMN(INDIRECT("G8:HI8"))-7,,1))=$A$2
with
N(OFFSET(INDIRECT("'"&EightiesNames&"'!G8:HI8"),,COLUMN(INDIRECT("G8:HI8"))-7,,1))=$A$2
Also, if E9:E300 contains numerical values, replace...
T(OFFSET(INDIRECT("'"&TRANSPOSE(EightiesNames)&"'!E9:E300"),ROW(INDIRECT("9:300"))-9,0,1))=$A$1
with
N(OFFSET(INDIRECT("'"&TRANSPOSE(EightiesNames)&"'!E9:E300"),ROW(INDIRECT("9:300"))-9,0,1))=$A$1
Here's the formula...
=SUM(IF(T(OFFSET(INDIRECT("'"&EightiesNames&"'!G8:HI8"),,COLUMN(INDIRECT("G8:HI8"))-7,,1))=$A$2,N(OFFSET(INDIRECT("'"&EightiesNames&"'!G9:HI9"),TRANSPOSE(MMULT(TRANSPOSE(ROW(INDIRECT("9:300")))^0,IF(T(OFFSET(INDIRECT("'"&TRANSPOSE(EightiesNames)&"'!E9:E300"),ROW(INDIRECT("9:300"))-9,0,1))=$A$1,ROW(INDIRECT("9:300"))-9,0))),COLUMN(INDIRECT("G9:HI9"))-7,,1)),0))
Hope this helps!
I am wondering, if there is a general way to express, that only visible rows of a formula should be taken into account.
If I have for example a formula sumif($E5:$E100; "ABC"; $F5:F100) it would be very helpful, if there would be a way to express, that the given ranges should only take visible cells into account. I could imagine that a kind of prefix can be specified to a range construct like % or that like. For example the formula then would look like sumif(%$E5:%$E100; "ABC"; %F5:%F100) to make clear, that in the given ranges only visible rows should be taken into account.
Same would then for example be for sum(%A1:%A100) which would mean, that in the range between A1 and A100 only visible cells should be taken to sum up the cells.
The point is, that this construct could be taken inside any kind of formula, no matter what it is.
Thanks in advance
Georg
Generically to sum sumrange based on a match in criteriarange.....but only for visible rows you can use this formula: =SUMPRODUCT((criteriarange=criteria)+0,SUBTOTAL(109,OFFSET(sumrange,ROW(sumrange)-MIN(ROW(sumrange)),0,1,1))) The first part (criteriarange=criteria)+0 just checks the criteria for each row and returns 1 for a match or 0 OFFSET returns an "array of ranges" with each range in this case being a single cell from the sum range. SUBTOTAL can process that and with the sum function (109) gives the "sum" (i.e. the value) of each cell, only when visible. – SUMPRODUCT then multiplies the two ranges and sums the result, effectively giving you the sum of visible rows where the criteria matches
Try This
=SUMPRODUCT(($E$5:$E$100="ABC")+0,SUBTOTAL(109,OFFSET($F$5:$F$100,ROW($F$5:$F$100)-MIN(ROW($F$5:$F$100)),0,1,1)))
I have a excel formula as below,
COUNTIFS($A$8:$A$14,$A8,$B$8:$B$14,$B8)
Here I want the criteria range to be calculated with a simple logic.
Instead of $A$14 I want this value to be calculated A$8+4 i.e. $A$14
In other words, it should get the current row and add 4 to be the criteria range for COUNTIFS
How can this be done is excel within the COUNTIFS formula?
Cheers
You could use =OFFSET(Cell Reference, Rows, Columns) within your formula to achieve this, for example:
=COUNTIFS($A$8:OFFSET($A$8,6,0),$A8, etc...)
Lets assume that the size of your range to be returned is stored in the cell D1.
=COUNTIFS($A$8:INDEX(A:A,ROW($A$8)+D1),$A8,$B$8:INDEX(B:B,ROW($B$8)+D1),$B8)
If you want the range to be defined as a certain number of rows after the current row as stated in your question, then still assuming the number of rows to be added is in D1, you would use the following:
=COUNTIFS($A$8:INDEX(A:A,ROW()+D1),$A8,$B$8:INDEX(B:B,ROW()+D1),$B8)
Expanding on Oliver's answer. The full format of OFFSET allows you to specify a whole range, not just one cell. OFFSET(Reference, Row Offset, Col Offset, Height, Width) so you could do:
OFFSET(A8,0,0,4,1)
This specifies the range A8:A11. In the COUNTIF
=COUNTIF(OFFSET(A8,0,0,4,1),A8,...)
Locking cells with $ works the same way within OFFSET if needed.
Try this:
=COUNTIFS(INDIRECT("$A$8:$A$" & 8+6),$A8,INDIRECT("$B$8:$B$" & 8+6),$B8)
Currently I have a huge formula in my excel sheet:
=SUM(SUMIF(INDIRECT(A9&"!$F:$F"),"working";INDIRECT(A9&"!$B:$B"))+SUMIF(INDIRECT(A9&"!$F:$F");"Open";INDIRECT(A9&"!$B:$B"))+SUMIF(INDIRECT(A9&"!$F:$F");"internal",INDIRECT(A9&"!$B:$B"))+(SUMIF(INDIRECT(A9&"!$F:$F"),"finished";INDIRECT(A9&"!$AP:$AP"))))
In A9 is the name of the excel sheet that the formula has to use. So for example in A3 there's '20140612', so it has to use the excel sheet with the name '20140612'. Furthermore it sums up some values depending on some conditions.
My question is: I would like to direct the columns in the other sheets by their headlines, not their positions in the sheet. So for example le column AP doesn't always has to be in the position AP, but has always the headline 'Points'.
Can you think of an adaption of the formula that can direct the column by their headline?
I though of the MATCH formula. But I'm not to sure where I have to put this in.
I think the simplest answer would be to use named ranges within your sheet. In this way you could name a range (currently in column AP) as 'Points', change your formula to use 'Points' instead of '$AP:$AP' & if you move your points data about the formulas would be unchanged.
If you are planning to keep changing your header row values then you could use HLOOKUP to match up the header column probably in conjunction with MATCH & INDEX.
To answer your question about the usage of MATCH(), it's worth thinking of it as half of a VLOOKUP() or HLOOKUP(), i.e. it's the bit that finds the row or column containing the value you're searching for, then you can use INDEX() to get that row / column from a range you specify.
So, if you know that one of your column headings is "Points", then you could find it by using:
=MATCH("Points", A1:Z1, FALSE)
...which would return 10 if "Points" were the heading of column J for example. If you wanted to then use that column for a lookup, you can use OFFSET() to define that column as a range to use for a lookup, so let's say I wanted to find the text "foo" in the "Points" column, I could use:
=MATCH("foo", OFFSET(A1:Z1000, 0, MATCH("Points", A1:Z1, FALSE) - 1, 1000, 1), false)
... which uses the column index I found before as an input to OFFSET() in order to dynamically reference J1:J1000 and then search for "foo" in that column.
For your example in the question, rather than A1:Z1000 you could use a call to INDIRECT() that would return the entire range of interest from your source sheet.