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

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

Related

INDEX MATCH not working on few cells (Same Formula)

After a lot of brainstorming I leared to write this forumula and it actually worked, however, when I tried to drag it down it did not work on some of the random cells in sheet and gave #REF error. I am not able to understand that when the value is there and the formula is working oo other cells then why on dragging it did not work on few.
Please helpm thanks in advance
Here is the formula
=INDEX(Volume!$D$3:$F$68,MATCH(B23,Volume!$A$3:$A$68,0),MATCH(D23,Volume!$D$2:$F$2,0),MATCH(E23,Volume!$B$3:$B$68,0))
Attached is the link to access Excel sheet because samme formula on dragging miss some of the random cells and shows #REF.
https://docs.google.com/spreadsheets/d/1fEyTc3SUAgumFr54HC2LVLoXGAbum9uG/edit?usp=sharing&ouid=102884722585384256735&rtpof=true&sd=true
It looks like you're trying to check three columns with an INDEX formula but your fourth argument is wrong (the 'MATCH(E23,Volume!$B$3:$B$68,0)' part). This argument selects which range of data should be used but you're giving it a value outside the range of data e.g. that part of the formula in cell H23 on the 'Sea Freight Rates' sheet returns 24 but there's no 24th column of data you've specified. The formula only works in some cases by pure accident and because of how the data on the 'Volume' sheet is laid out.
This is the formula that you want to use in cell H2 and then drag down. The 'MATCH($B2&$E2' part joins the value in column B and E together to give a unique combination (e.g. 'CNNBO PT20DC' for cell H2) while the 'Volume!$A$2:$A$68&Volume!$B$2:$B$68' part produces an array of all possible combinations from the relevant columns in the 'Volume' sheet. It will look for 'CNNBO PT20DC' in this array and return the relevant row number. The formula then looks for the correct 'Destination Port' like you have before which gives the relevant column number. You'll then get the correct value and that row and column:
=INDEX(Volume!$D$2:$F$68,MATCH($B2&$E2,Volume!$A$2:$A$68&Volume!$B$2:$B$68,0),MATCH($D2,Volume!$D$2:$F$2,0))
Side-note, you have two rows for INNSA PT and 20DC on the 'Volume' sheet (rows 6 and 22) - is that correct?

How can I get Excel to generate columns dynamically, according to the values in a different column?

In Excel 2016, I have a "Brands" column, each of its rows containing a string. The set of possible string values is limited, and they may appear more than once.
There is related data in another column, "Models". Each model value is always different.
How can I get Excel to generate a column for each existing brand and populate it with the corresponding Models in an automatic way?
My problem is that I can't do it manually because for each analysis, the amount of Brands is different and I don't know it beforehand.
This would be the input:
And this is the expected output:
Any ideas?
Thank you in advance!
Try using an array formula. First, put the distinct brand values on row 1, starting in column D. You can use column C if you want, but I like to have an empty column between the data and the desired results.
D1 = Apple, E1 = Nokia, F1 = Samsung, G1 = Xiaomi
Then, just below Apple in cell D2, paste this formula:
=IFERROR(INDEX($B$2:$B$9999, SMALL(IF(D$1=$A$2:$A$9999, ROW($A$2:$A$9999)-ROW($A$2)+1), ROW(1:1))),"")
If you have more than 9999 rows, then adjust as needed in the formula.
With the cursor still in the formula, make it an array by simultaneously pressing CTRL-SHIFT-ENTER.
Copy the formula across to cells E2, F2 and G2. You may need to repeat the array trick (CTRL-SHIFT-ENTER) for each of those again if things look wrong. So, your excel will look like this:
Now, drag the formulas down as far as you need. The iferror part of the formula will ensure that cells look clean if no more models are found.
---EDIT AFTER RECENT COMMENT---
I cannot determine how to automatically pick distinct values from column A and automatically convert them to a row. It's easy to keep it in a column, but the transpose to row is troubling.
At any rate, here's the ugly update. Cell D1 would simply state "Brands". In cell D2, make this an array formula (CTRL-SHIFT-ENTER).
=IFERROR(INDEX($A$2:$A$9, MATCH(0, COUNTIF($D$1:D1, $A$2:$A$9), 0)), "")
So, row 2 will be your brands. Drag the formula across, repeat the array trick.
Now, in cell E1, type formula =D2. Drag across.
Place the formula suggestion from the original answer, starting in row 3. End result looks like this below. It should be "automated" now, but it's not appealing. Minor edits will help (making row 1 nearly invisible, for example).

Sum column dynamically

Having A1+C1 and B1+D1 in two cells how can I dynamically set up a formula to catch if some column is added.
Let's say the user adds two columns in the middle. I should have A1+C1+E1 and B1+D1+F1.
I thought it would have been automatic but it is not.
Replace:
=A1+C1
By:
=SUM(A1:C1) - B1
In case you want to check if the column number is divisble by three, you can use following formula:
=IF(MOD(COLUMN(A1);3)=0;A1;0) // I've put the values from 1 to 10 in A1-J1
// and I've dragged this formula from A2 to J2,
// the values were 0,0,3,0,0,6,0,0,9,0.
Unfortunately I don't have a simple way to sum those values in one easy formula.
If you always add two columns then A1+C1 will always be looking at odd number columns and B1+D1 will always be looking at even numbered columns.
{=SUM(IF(ISODD(COLUMN($A$1:$D$1)),$A$1:$D$1))}
and
{=SUM(IF(ISEVEN(COLUMN($A$1:$D$1)),$A$1:$D$1))}
As long as you insert columns between A:D the ranges will extend to accommodate.
Edit:
Based on the comment that row 2 contains codes and row 3 contains the figures to add up for each code then this array formula will work:
{=SUM(IF($A$2:$J$2="H1",$A$3:$J$3))}
Edit2: and if I wake up you can even use the non-array and built in formula:
=SUMIF($A$2:$J$2,"H1",$A$3:$J$3)
The H1 text can be changed to another code or to a cell reference containing the code to get the sum of values in row 3 for the specified code.
As an array formula it must be entered using Ctrl+Shift+Enter.

Excel: SUMIF refered to dynamic range

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)))

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