Want SUMIFS to compare two columns row by row - excel-formula

I wasn't sure how to explain it in the title, but here's my problem:
I want to sum the values of every row in column A when, in the same row, the value in col B is equal to the value in col C. For example =SUMIFS(A:A,B:B,"="&C2) seems to work, but I don't want to compare to any specific cell, but the C-cell on that specific row, where the potential A-value is.
Edit: I could of course make an additional column with "If B2=C2" and so on by dragging it down, but this is what I want to avoid.
I also need to know the number of rows that is included in the sum.

Try:
= SUMPRODUCT((A:A)*((B:B)=(C:C)))
But you should limit the full ranges to where ever your data ends, e.g.
= SUMPRODUCT((A1:A100)*((B1:B100)=(C1:C100)))

I would suggest to limit the ranges but this array formula works:
=SUMPRODUCT(A:A,IF(B:B=C2,B:B))
This must be applied with Ctrl+Shift+Enter.
But now that I read your question again it seems that you just need:
=SUMIFS(A:A,B:B,C2)

Related

Condensing nested if-statements with multiple criteria

The blue columns is the data given and the red columns is what is being calculated. Then the table to the right is what I am referencing. So, F2 will be calculated by the following steps:
Look at the Machinery column (D), if the cell contains LF, select column K, otherwise select column L
Look at the Grade column (E), if the cell contains RG, select rows 4:8, otherwise select rows 9:12.
Look at the Species column (A), if the cell contains MS, select rows 5 and 10, otherwise.......
Where every the most selected cell is in columns K and L, copy into column F.
Multiply column F by column C.
I don't want to make another column for my final result. I did in the picture to show the two steps separately. So column F should be the final answer (F2 = 107.33). The reference table can be formatted differently as well.
At first, I tried using nested-if statements, but realized that I would have like 20+ if statements for all the different outcomes. I think I would want to use the SEARCH function to find weather of not the cell contains a specific piece of information. Then I would probably use some sort of combination of match, if, v-lookup, index, search, but I am not sure how to condense these.
Any suggestion?
SUMPRODUCT is the function you need. I quickly created some test data on the lines of what you shared like this:
Then I entered the below formula in cell F2
=SUMPRODUCT(($I$4:$I$9=E2)*($J$4:$J$9=LEFT(A2,FIND(" ",A2)-1))*IF(ISERROR(FIND("LF",D2,1)),$L$4:$L$9,$K$4:$K$9))
The formula may look a little scary but is indeed very simple as each sub formula checks for a condition that you would want to evaluate. So, for example,
($I$4:$I$9=E2)
is looking for rows that match GRADE of the current row in range $I$4:$I$9 and so on. The * ensures that the arrays thus returned are multiplied and only the value where all conditions are true remains.
Since some of your conditions require looking for partial content like in Species and Machine, I have used Left and Find functions within Sumproduct
This formula simply returns the value from either column K or L based on the matching conditions and you may easily extend it or add more conditions.

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 Check for multiple value in one row within a range

I have a spreadsheet with thousands of rows and numerous columns. What I am hoping to accomplish is to basically check column AB for a certain value. When it finds a row that contains (not exact match, just needs to include) the word "test" then I want to check if column Z (in the same row) has value "next". If both those are true, then I want to display the value from column N in that same row. What code could I use to accomplish this?
Note: I need this to list the value from column N for each row that has both "test" and "next", not just for one row.
Edit:
To clarify what I mean by multiple values see this image:
I want a formula that will list ALL of the Column N values of rows that meet have both test and next. And this list is not in the row, it is going to be one column listing each match.
Update:
I have gotten close to getting this done, this is my best formula up to this point. =ArrayFormula(IF(ISNUMBER(MATCH("*"&$A$1&"*",SID!AB:AB,0)),SID!N3,"n")) This works once, but here is what I need fixed:
This returns the first value that works, in this example Place1, but then it just lists everything form column C, even if it does not reach the requirements. Most likely an issue with my arrayformula.
Try out the below formula,
=IF(AND(ISNUMBER(MATCH("*"&"test"&"*",A1,0)),ISNUMBER(MATCH("*"&"next"&"*",B1,0))),"Column N value","")
This formula searches cell A1 for the string test (anywhere in it) and searches next in cell B1. I leave the part of replacing the cells with the required columns to you and also change the Column N value in the formula to N1

Sort Order formula to alphabetise in Excel

I am currently drawing up a spreadsheet that will automatically remove duplicates and alphabetize a list:
I am using the COUNTIF() function in column G to create a sort order and then VLOOKUP() to find the sort in column J.
The problem I am having is that I can't seem to get my SortOrder column to function properly. At the moment it creates an index for two number 1's meaning the cell highlighted in yellow is missed out and the last entry in the sorted list is null:
If anyone can find and rectify this mistake for me I'll be very grateful as it has been driving me insane all day! Many thanks.
I'll provide my usual method for doing an automatic pulling-in of raw data into a sorted, duplicate-removed list:
Assume raw data is in column A. In column B, use this formula to increase the counter each time the row shows a non-duplicate item in column A. Hardcord B2 to be "1", and use this formula in B3 and drag down.
=if(iserror(match(A3,$A$2:A2,0)),B2+1,B2)
This takes advantage of the fact that when we refer to this row counter in our revised list, we will use the match function, which only checks for the first matching number. Then say you want your new list of data on column D (usually I do this for display purposes, so either 'group-out' [hide] columns that form the formulas, or do this on another tab). You can avoid this step, but if you are already using helper columns I usually do each step in a different column - easier to document. In column C, starting in C3 [C2 hardcoded to 1] and drag down, just have a simple counter, which error-checks to the stop at the end of your list:
=if(C2<max(B:B),C2+1," ")
Then in column D, starting at D2 and dragged down:
=iferror(index(A:A,match(C2,B:B,0)),"")
The index function is like half of the vlookup function - it pulls the result out of a given array, when you provide it with a row number. The match function is like the other half of the vlookup function - it provides you with the row number where an item appears in a given array.
Hope this helps you in the future as well.
The actual reason that this is going wrong as implied by Jeeped's comment is that you can't meaningfully compare a string to a number unless you do a conversion because they are stored differently. So COUNTIF counts numbers and text separately.
20212 will give a count of 1 because it is the only (or lowest) number.
CS10Z002 will give a count of 1 because it is the first text string in alphabetical order.
Another approach is to add the count of numbers to the count if the current cell contains text:-
=COUNTIF(INDIRECT("$D$2:$D$"&$F$3),"<="&D2)+ISTEXT(D2)*COUNT(INDIRECT("$D$2:$D$"&$F$3))
It's easier to show the result of three different conversions with some test data:-
(0) No conversion - just use COUNTIF
=COUNTIF(D$2:D$7,"<="&D2)
"999"<"abc"<"def", 999<1000
(1) Count everything as text
=SUMPRODUCT(--(D$2:D$7&""<=D2&""))
"1000"<"999"
(2) Count numbers before text
=COUNTIF(D$2:D$7,"<="&D2)+ISTEXT(D2)*COUNT(D$2:D$7)
999<1000<"999"
(3) Count everything as text but convert numbers with leading zeroes
=SUMPRODUCT(--(TEXT(D$2:D$7,"000000")<=TEXT(D2,"000000")))
"000999" = "000999", "000999"<"001000"

Excel formula to return a value from a range where conditional statements are true

I've searched for an answer for this for awhile now but haven't had any luck. It's a bit difficult to put into words, so please let me know if anything's unclear.
What I want is a way to perform conditional statements on two columns. When both conditions are true, I want to return the value from a third column that is in the same row as where these conditions were met. So if the conditions are true in D5 and C5, I want to return the value from Q5 into the cell with the formula. The conditional statements are easy enough but I haven't figured out the last step yet.
If I use the following formula, I get 1 in the cell with the formula, which is right.
=IF(AND('Stata Output'!D2:D6='Backcheck schedule and results'!C2, 'Stata Output'!C2:C6=1),1,0)
However, replacing 1 in the formula with the range of cells that I want to pull the value from gives me a zero every time, which I know is wrong. For this latter formula I'm using:
=IF(AND('Stata Output'!D2:D6='Backcheck schedule and results'!C2, 'Stata Output'!C2:C6=1),'Stata Output'!Q2:Q5,0)
Any ideas on where I'm messing up?
Thanks!
Give this a shot - I'm sure there is a simpler way but it seems to get the job done (make sure to enter as an array formula with Control+Shift+Enter):
=INDEX('Stata Output'!$D$2:$Q$6,MATCH(1,(--('Stata Output'!$C$2:$C$6=1)*--('Stata Output'!$D$2:$D$6="Backcheck schedule and results")),0),14)
What this does is take your entire table as the range (first argument to INDEX, then uses MATCH to find the row where both of your conditions are met (in this case, column C must be 1 and the corresponding value in column D must be Backcheck schedule and results). You then search the resulting array for 1 (which indicates the match) and return column 14 (which represents column Q in our table). You can continue to add filters inside the MATCH() function as needed.
Note that this will match only the first 'hit'.
Looks like AND isn't working correctly in array formulas, so I would use this:
=IF(('Stata Output'!D2:D6='Backcheck schedule and results'!C2)*('Stata Output'!C2:C6=1)=1,'Stata Output'!Q2:Q5,0)
This will give you a new results column, that will be filled from the Q2:Q6 column for every matching row.
To use it, you should select all of the results column, input the formula and then use CTRL+SHIFT+ENTER.
Just make sure to use use ranges of the same size.
Try the following formula:
=LOOKUP(2,1/('Stata Output'!D2:D6='Backcheck schedule and results'!C2)/('Stata Output'!C2:C6=1),'Stata Output'!Q2:Q5)
If there is more than one row where both conditions are satisfied, this formula will return the col Q value from the last of those.

Resources