Excel: SUMIF refered to dynamic range - excel-formula

I need to write a SUMIF formula with reference to dynamic columns.
Here an example.
In the cell G7, I need to have the sum of the number related to Mark (row 3) from the column stored in the cell D3 (in this case H) to the column stored in the cell D4 (in this case L). The result of this formula gives the number 7 in cell G7 and the number 8 in G8.
I would like to manage this issue without using VBA if possible.
I thought at something like:
G7 = =SUMIF(F:F;F7;<the value stored in D3>:<the value stored in D4>)
I hope to have explained the issue well.

A simple sum with two INDEX/MATCH will do it:
=SUM(INDEX($1:$10000,MATCH(F7,F:F,0),CODE(UPPER($D$3))-64):INDEX($1:$4,MATCH(F7,F:F,0),CODE(UPPER($D$4))-64))
On note, this will only work till column Z, if you data is past that we will need to find something different than the CODE(UPPER($D$3))-64 to denote the column extents.
We would need to use INDIRECT, which if can be avoided, should. It should be avoided because it is volatile. So if your data goes past column Z then use:
=SUM(INDEX(INDIRECT($D$3&":"&$D$3),MATCH(F7,F:F,0)):INDEX(INDIRECT($D$4&":"&$D$4),MATCH(F7,F:F,0)))

Related

How can i calculate the SUM of various cells within a COUNTIFS?

I have a database in excel where columns hold lots of different pieces of data. On a different sheet I would like to be able to perform a SUM within a COUNTIFS statement.
The logic in my head is this, but obviously it does not work:
=COUNTIFS($B:$B,$F4,(SUM($C:$C+$D:$D)),">=2")
So here I am saying if in column B of the database the value is equal to what I have in cell F4 on my sheet, then I would like, for each row where the value is what I have in F4, to add up the values between cells C and D, and if that value is greater than 2 then count it, if not then do not count it.
Here is a picture of expected output:
Here is an option based on your sample data, however by the looks of it I get different results than yours:
Formula in G4:
=SUMPRODUCT(($B$4:$B$12=$F4)*($C$4:$C$12+$D$4:$D$12>=G$3))
Note: I changed cell's G3:H3 value to be able to reference them.
Drag down and right. For a dynamic reference style you might try:
=SUMPRODUCT((F2:INDEX(F:F,COUNTA(F:F))=C3)*(H2:INDEX(H:H,COUNTA(H:H))+I2:INDEX(I:I,COUNTA(I:I))>=2))
Using your layout
Using a helper column in column E
=C4+D4
Then drag that down
Putting this formula in G4 and dragging down should do what you want
=COUNTIFS(E:E;$G$3;B:B;F4)
and this in H4
=COUNTIFS(E:E;$H$3;B:B;F4)
As someone else pointed out, your sample expected result is incorrect if I understand what it is that you want to achieve correctly. You can change the Value in G3 and H3 respectively to adjust your criteria if necessary. In my test i had >=2 and >=3 respectively

What's an alternative to using an IF function for long strings of formulas in Excel?

The case I'm working on requires that I apply one of six different formulas on column W for a long list of rows depending on the corresponding row value on column C.
For example, if cell C5 says "A", my formula on W5 must be
=(SUMIFS(K:K,A:A,A5,F:F,F5)*8).
Then, if C6 = "B", W5
=SUMIFS(L:L,A:A,A6,F:F,F6), etc.
As I mentioned, I have 6 of these formulas which are way longer than the above as here I just stated them as an example, so it's cumbersome to write all the formulas in an IF format i.e.
=IF(C5="A", *bigassformula1*,if(c5=B, *bigassformula2*...)))...
Is there any way to do this differently, either through Formulas or VBA?
One thing you can do is create a table of equations and look them up based on the value like so:
=VLOOKUP(G7,EquationLookups,2,FALSE)
You would want to put the value you're searching for in the first column, and the formula you want to use in that case in the second column.

indirect reference for sum a range

I want to have a sum formula in a cell such as =SUM(Ex:Ey) while E is the column and x and y are row numbers. Is there a way I can have excel to match x and y to the numbers in other cells? For example: cell D1=3 and D2=12 and there are many numbers in column E. By the end of column E, I want to have a cell that sum only the rows from the numbers of D1 to D2, which is sum of E3 to E12 in this example. The idea is that I can change D1 and D2 to change what rows in column I want to sum.
You can use INDIRECT to reference a range using a constructed string address - SUM(INDIRECT("E"&D1&":E"&D2)).
"E"&D1&":E"&D2 will give you the string "E3:E12" in your example, which INDIRECT will then convert to a reference to that actual range.
Note that INDIRECT comes with a recalculation overhead, but will be fine if you aren't doing too many of them or too complex things!
Another way to achieve this is to use the sumif formula or sumifs for multiple conditions. This works better in some situations and can be easier to read and audit\review.

Adding all the values below the current cell in Excel

I am trying to display the total sum of all the numbers for a particular column. I want the sum to be displayed above the column as follows:
21 30
A B
6 5
6 10
6 10
3 5
I know I can sum the values and display it at the bottom of the column using =SUM(A3:INDIRECT("D"&ROW()-2)), however I am not getting a way to display it at the top of the column.
Please guide.
Based on the comments and the previous answers I suggest following formula, entered in cell A1:
=SUM(OFFSET(A$2,0,0,ROWS(b:b)-1))
You can then copy/paste to the right till second last column.
You could also modify your formula in A1 like this to achieve the same:
=SUM(INDIRECT("A2:A"&ROWS(A:A)-2))
But then you cannot copy/paste to the right...
A more general approach with your idea would be:
=SUM(INDIRECT(ADDRESS(ROW()+1,COLUMN())&":"&ADDRESS(ROWS(A:A),COLUMN())))
You can then copy/paste to the right till last column.
Some explanations:
Both formula sums up every value in the range from A2 till the bottom of column A (i.e. for Excel 2010 this would be A2:A1048576)
It doesn't matter if there are blanks or cells without value; the formula sums up only the numbers
I put A$2 and B:B in the OFFSET formula to avoid circular references, since I'm writing in cell A1 and I cannot write A$1 nor A:A
With the INDIRECT formula you don't have to worry about circular references
Further commenting (sorry, I don't have the credits to comment at the right place under the question):
Phylogenesis formula =SUM(A3:A65535) could also do the work, isn't it?
Didn't understand your question at first, because you talk of "sum of all the numbers for a particular row" but then you sum columns, isn't it?
When I'm doing something like this, I prefer to not include any empty cells beneath the range I'm summing, because I've had errors in the past as the result of including them (usually because there's a cell way down in the column somewhere that I'm not expecting to have a value). I'm assuming that A & B are your column headers. Assuming that, here is how I would do it. This is your formula for cell A1:
=SUM(OFFSET(A$1,2,0,COUNTA(A$3:A$65535)))
Explanation
I'm updating this with a brief explanation, per the OP's request.
According to ExcelFunctions.net:
The Excel Offset function returns range of cells that is a specified number of rows and columns from an initial supplied range.
The function reference for OFFSET is:
=OFFSET(reference, rows, cols, [height], [width])
What this formula does is create a dynamic range based on the number of cells in the selection, relative to cell A$1. This is an offset of two rows and no columns, which starts the range at A$3. The height of the range is the total number of filled cells in the range A$3:A$65535. The assumption here is that there are no blank cells in the range, which there were not in the sample data.

Excel SUMIF function sums multiple and/or wrong column.

I'm having an issue getting accurate data from the SUMIF function. This appears to be caused by the SKU and Product name being identical however I don't understand why the selected range would be ignored.
SUMIF(G:K,A2,K:K) - Cell D2 is calling for the sum of K yet returning the sum result of K2:M2. All other results in D are correct.
SUMIF(G:K,A2,I:I) - If I change the formula in D to SUM I:I (text not a numeric field) the function returns the sum of K:K
Example file http://tempsend.com/013C2B6378
According to the documentation here the range to be summed starts at the top left of the sum range (K:K in your first example) but its size is given by the size of the criteria range (G:K in your example). So I think that's why you're getting extra columns summed in your result.
If you have multiple criteria involving different columns, you should be able to use SUMIFS.
So let's say your data sit in 8 rows (including the headings).
then you simply need to change your formula to say, look for B2 in column G OR in I, if true, then sum the values in K. Right?
put this formula in B2 and press ctrl+shift+enter to calculate the formula.
=SUM(IF(($G$2:$G$8=B2)+($I$2:$I$8=B2),1,0)*$K$2:$K$8)
then drag and fill down until the last cell.
obviously you need to adjust the ranges in the formula to adapt to your own data.
tell me if you get to the answer via this.

Resources