Conditional sum - excel

I need a quick macro/formula. I have two columns:
A | B
|
| 10 | 9 |
| 11 | |
| 8 | 10 |
| N/A |
| 4 | |
| 7 | 8 |
total | 40 | 27 |
Now I want to add a sum to a column that adds up all the values in column A BUT replaces that value with the value in column B if it exists.
So I would expect the sum to end up being 9 + 11 + 10 + 4 + 8 = 42
I'm not entirely sure about the power of Excel's calculations or how to go about performing a for loop within a formula.

There are MANY ways to do this, but I would do it as two sums added together....
Assuming you had the data you supplied in your question in cells A1 to B6, your formula could look as follows:
=SUMIF(B1:B6,"",A1:A6)+SUM(B1:B6)
In essence, you're saying:
Sum A1:A6 anywhere B1:B6 is blank
PLUS
Sum B1:B6
Hope this makes sense and helps!

=SUM(IF(ISNUMBER(B1:B5),B1:B5,A1:A5))
will do this. Note that the formula is an array formula though; once you've finished entering it in a cell, press Ctrl + Shift + Return, rather than Return. That tells Excel that the formula is an array type.
I've set this up for 5 rows; just extend as you need.

One of many other possibilities could be a helper column containing:
=IF(ISBLANK(B3),A3,B3)
copied down (and up, if necessary!)
Not necessarily better than many alternatives but perhaps easier to see 'what is going on' and =IF may already be familiar.

Related

Sum All VLOOKUP Matches

I have a sheet where I am recording what I eat:
Another where I keep an index of values to lookup
I tried
=SUM(VLOOKUP('Sheet1'!A2:A11,'Sheet2'!A2:E11,2,FALSE))
but that only returned the first match, so then I tried
=SUMPRODUCT(SUMIF('Sheet1'!A2:A11,'Sheet2'!A2:A11,'Sheet2'!B2:B11))
but that isn't working either.
does anyone have a solution, where I can also multiply the value of the return match by the # of servings in the first sheet?
Thanks!
If you want a single output of calories through SUMPRODUCT then you can use
=SUMPRODUCT(B2:B11*IFERROR(VLOOKUP(A2:A11,Sheet2!A2:B11,2,0),0))
If you are sure that all entries on Sheet 1 can be located on Sheet 2 then you can drop IFERROR portion like
=SUMPRODUCT(B2:B11*VLOOKUP(A2:A11,Sheet2!A2:B11,2,0)).
Beware that if a value is not found in Sheet 2 then formula will produce wrong result as IFERROR will multiply the serving quantity with 0.
I combine 2 tables into one sheet, Table 1 housed in Column A & B and Table 2 housed in Column D & E
In G2, "Total Serving Colories" enter formula :
=SUMPRODUCT(VLOOKUP(T(IF({1},A2:A12)),D2:E12,2,FALSE)*B2:B12)
It's not super-clear what you're trying to get at. But defining the "Calories Per Serving" in a range called "cals",
+---+---------+-----+--------------------------------+
| | A | B | C |
+---+---------+-----+--------------------------------+
| 1 | egg | 3 | =(VLOOKUP(A2,cals,2,FALSE))*B2 |
| 2 | oatmeal | 1.5 | =(VLOOKUP(A3,cals,2,FALSE))*B3 |
| 3 | shrimp | 2 | =(VLOOKUP(A4,cals,2,FALSE))*B4 |
+---+---------+-----+--------------------------------+
Results in:

Calculate sum of differences between rows but only if the previous cell is bigger than the current one

I started to get a headache around my problem that I cannot figure out for the love of me.
There are unknown amounts of column if that makes any difference, but basically each row needs to be compared to the previous one and ONLY when the previous value is greater, the difference between them gets added to the sum.
So for example I have this table
| A |
--|-----|
1 | 100 |
2 | 90 |
3 | 80 |
4 | 100 |
5 | 70 |
6 | 20 |
7 | 100 |
...
Expected result: 100, derived from ((100-90) + (90-80) + (100-70) + (70-20))
I have spent a whole day browsing every single excel tutorial page and cannot find a single helpful answer. Please help :(
Formula for Cell B2: (pull down through the rows).
=IF(A1>B1;A1-B1;0)+B1
Logic: If previous value is larger than current value, add the difference to the total.
If you want to do it in one formula, a basic way would be two use two ranges offset by one cell:
=SUMPRODUCT((A1:A6-A2:A7)*(A1:A6>A2:A7))
If you wanted to make a bit more dynamic (assuming there are no gaps in the data) you could try
=SUMPRODUCT((A1:INDEX(A:A,COUNT(A:A)-1)-A2:INDEX(A:A,COUNT(A:A)))*(A1:INDEX(A:A,COUNT(A:A)-1)>A2:INDEX(A:A,COUNT(A:A))))
If there are blanks between numbers, this won't work and you would probably need to go back to a simpler pull-down formula

Excel countif and sumif together

I am trying to write a formula in Excel which will count how many times we have sold less than 50 of a particular product. For example, here is a day's sales:
Order | Product | Qty
1 | A | 5
2 | A | 5
3 | A | 5
4 | B | 30
5 | C | 75
I want a formula in a cell which says how many times we have a requirement for less than 50 of a certain product. So in the example above, there is a total of 15 As, 30 Bs and 75 Cs, so 2 of those are less than 50.
I think it will need to be an array function of COUNTIF and SUM, but can't figure it out.
You could use this formula:
=SUMPRODUCT(--(IF(ROW($B$2:$B$10)=MATCH($B$2:$B$10,$B$1:$B$10,0),SUMIF($B$2:$B$10,$B$2:$B$10,$C$2:$C$10),"")<50))
Note: It's an array formula and must be entered through Ctrl+Shift+Enter
Product order placement can be randomized and does not have to be in order.
Another way
=SUMPRODUCT((SUMIF(B2:B10,B2:B10,C2:C10)<50)/COUNTIF(B2:B10,B2:B10))
Maybe something like that will help:
=SUMPRODUCT(--IF($B$2:$B$11<>$B$1:$B$10,SUMIF($B$2:$B$11,$B$2:$B$11,$C$2:$C$11)<50,0))
Note that this is an array formula so needs to be entered with Ctrl+Shift+Enter. Data needs to be sorted by Product (i.e. product A cannot appear in random rows, like row 2, 20 and 100; it needs to be grouped together).
Result:

Execute range function (e.g. sum, average, median) based on a condition

I am trying to do something in Excel that seems easy but I cannot figure out which functions to use. I want to calculate the median value of a range of cells, for which it is true that an adjacent cell contains a specific word.
For instance, take these two columns
yes | 1
no | 4
no | 5
yes | 3
yes | 2
no | 9
no | 6
no | 8
yes | 1
no | 5
yes | 3
no | 5
Let's say I want to calculate the median for all no-cases, and the median for all yes cases. It would look like this (if I did it correctly):
yes | 2
no | 5
I know that for taking the sum, you can use COUNTIF, and I also know that there is an IF function, but I do not know how to use it to target adjacent cells to the one you want data from.
Use the array formula:
=MEDIAN(IF(A1:A12="yes",B1:B12))
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
(same for the "no" case)

SUMIF of multiple columns with INDIRECT

I have a Excel sheet which is used as database, let's call that MyDB in the following Example. The first column A consists of some strings.
A | B | C
-----------------|--------------|------------------------------------------
Turnover 2014 | 1 | 2
Something | 2 | 0
Something | |
Turnover 2014 | 3 | 1
Something | |
Something | 0 | 2
What I want to do is look for the string Turnover 2014 and sum all values in that row from B:C (C is just an example in my case it will be variable and can be F or M).
What I have:
=SUMIF(INDIRECT("'MyDB'!A"&Helper!D2&":"&"A"&Helper!D8),"=Turnover 2014",INDIRECT("'MyDB'!$B"&Helper!D2&":"&"B"&Helper!D8))
The Helper!D2 and Helper!D8 contain the variable range, which is one of the reasons I have to use INDIRECT. For this example lets assume D2 = 1 and D8 = 6 (the full table)
Simple version:
=SUMIF(INDIRECT("'MyDB'!A1:A6"),"=Turnover 2014",INDIRECT("'MyDB'!B1:B6"))
This sums all values in B where A = Turnover 2014, so no problem here. Now I will show you my attempts to do the same with multi-columns:
=SUMIF(INDIRECT("'MyDB'!A1:A6"),"=Turnover 2014",INDIRECT("'MyDB'!B1:C6"))
=SUMPRODUCT((INDIRECT("'MyDB'!A1:A6") = "Turnover 2014")*(INDIRECT("'MyDB'!B1:C6")))
Both didn't work in my case (IMPORTANT I'm not talking about the simplified version I'm talking about the original version with all the variables).
In all cases I only get the sum of 4 where I need 7
Check whether your column A contains Turnover 2014 without leading/trailing spaces.
And try:
=SUMPRODUCT(
(TRIM(INDIRECT("'MyDB'!A"&Helper!D2&":"&"A"&Helper!D8)) = "Turnover 2014")*
(INDIRECT("'MyDB'!B"&Helper!D2&":"&"C"&Helper!D8))
)
also I suggest you to take a look at alternative formula without INDIRECT which is much better because it's not volatile formula:
=SUMPRODUCT(
(TRIM(INDEX(MyDB!$A:$A,Helper!D2):INDEX(MyDB!$A:$A,Helper!D8))="Turnover 2014")*
(INDEX(MyDB!$B:$B,Helper!D2):INDEX(MyDB!$C:$C,Helper!D8))
)

Resources