Excel formula to sum as long as - excel

I'm trying to find a formula to calculate the balance of a column until a negative value is found. After the negative value is found, the balance must be calculated again until the next negative value. Basically tracking what you spent, except it only shows a value when you sold something. Anybody have an idea if this is possible to do in MS excel? Thanks!

OK. Now I get your question. I think the following will do the trick. The results exactly match your example.
// In these cells only
F2: =MAX(0,B2*C2)
G2: =MAX(0,B2)+MIN(0,B2)
// In these cells, then copy down
E3: =IF(B3<0,D3-(F2-F3),"")
F3: =F2+MAX(0,B3*C3)+IF(G2=0,0,MIN(0,B3*F2/G2))
G3: =G2+MAX(0,B3)+MIN(0,B3)
I would note a couple of things about this:
1) You might consider changing the names of your columns to trans, quan, $ per, $ ttl, $ gp, and name the 2 columns I am adding $ inv and inv.
2) This is using the average cost of inventory, recalculated with each transaction, not LIFO or FIFO.
3) If entries get out of order such that quan goes negative, I think this solution will fail. In any case, that might be an error you'd want to notice.
4) FYI, the "IF(G2=0" part of F3 is only there to avoid a divide by 0 error when G2 (inventory) is 0. I could have done this other ways, of course. It works.
5) I've left E2 blank on the assumption that you can't sell as you've not bought.

One way would be to add 2 additional columns, which could be hidden or on another sheet, then (here assuming adding columns F2 and G2 added at the right):
In Cell E2: =IF(B2<0,G2,"")
In Cell F2: =B2*C2
In Cell G2: =SUM(F$2:F2)
Copy these down and, assuming I understand your question correctly, you'll get the results you desire.

The biggest problem you've got with this is working out the ranges to check for the balance calculation. This won't work if the next row in your table is a 'sell' while you've still got one 'apple' left from the previous purchase. If you want to do anything more convoluted you should use VBA.
Others will probably have an easier way to work out the ranges; I did it in a rather convoluted way and don't have time to optimise them.
In column F there's an array formula to calculate the last row in the range of 'buys' relevant to that 'sell'.
=IF($B2>=0,"",LARGE(IF($B$2:$B2>0,ROW($A$2:$A2)),1))
In column G there's a normal formula to capture the row number of the first row in the range.
=IF(ROW()=2,ROW(),IF(B2>0,IF(B1<0,ROW(),""),""))
In column H, convert this to be stored in the relevant 'sell' row using an array formula:
=IF($B2>=0,"",LARGE(IF($G$2:$G2>0,$G$2:G2),1))
In column E, balance, use these calculated row range references in an INDIRECT statement to calculate the balance.
=IF(B2>0,"",(C2*-(B2))-(-(B2)*(SUMPRODUCT(INDIRECT("B"&H2&":B"&F2),INDIRECT("C"&H2&":C"&F2)/SUM(INDIRECT("B"&H2&":B"&F2))))))
Note that INDIRECT uses a string to reference cells and ranges and if you move cells or ranges you will have to manually change the INDIRECT string to reference the correct cells.

Responding to #carol, and looking at the Q&A again, specifically where you say " although the problems comes up after because it needs to ignore the balances that came before José and start with the new ones that follow up," I realize that you may be looking to instead display the balance of all sales receipts and purchases since the last sale. If so:
In Cell G2: =F2
Do not copy down the above. Do copy down those below.
In Cell E2: =IF(B2<0,G2,"")
In Cell F2: =B2*C2
In Cell G3: =IF(B2<0,F3,F3+G2)

Related

How to simplify 64 levels of nesting formula in Excel?

I encountered "64 levels of nesting" issue while working on my formula. Is there any way to simplify this formula? This formula worked fine up from 10 to 500 (G:G) but once over 510 something, it encountered "64 levels of nesting" issue. I found some solutions that involved lookup and match-index, but I can't see where/how to implement it here.
Formula is in cell B3:
=IF(B2<=$G$2,B2+($E$2*B2),IF(AND(B2>$G$2,B2<=$G$3),B2+($E$3*B2),IF(AND(B2>$G$3,B2<=$G$4),B2+($E$4*B2),IF(AND(B2>$G$4,B2<=$G$5),B2+($E$5*B2),IF(AND(B2>$G$5,B2<=$G$6),B2+($E$6*B2),IF(AND(B2>$G$6,B2<=$G$7),B2+($E$7*B2),IF(AND(B2>$G$7,B2<=$G$8),B2+($E$8*B2),IF(AND(B2>$G$8,B2<=$G$9),B2+($E$9*B2),IF(AND(B2>$G$9,B2<=$G$10),B2+($E$10*B2),IF(AND(B2>$G$10,B2<=$G$11),B2+($E$11*B2),IF(AND(B2>$G$11,B2<=$G$12),B2+($E$12*B2),IF(AND(B2>$G$12,B2<=$G$13),B2+($E$13*B2),IF(AND(B2>$G$13,B2<=$G$14),B2+($E$14*B2),IF(AND(B2>$G$14,B2<=$G$15),B2+($E$15*B2),IF(AND(B2>$G$15,B2<=$G$16),B2+($E$16*B2),IF(AND(B2>$G$16,B2<=$G$17),B2+($E$17*B2),IF(AND(B2>$G$17,B2<=$G$18),B2+($E$18*B2),IF(AND(B2>$G$18,B2<=$G$19),B2+($E$19*B2),IF(AND(B2>$G$19,B2<=$G$20),B2+($E$20*B2),IF(AND(B2>$G$20,B2<=$G$21),B2+($E$21*B2),IF(AND(B2>$G$21,B2<=$G$22),B2+($E$22*B2),IF(AND(B2>$G$22,B2<=$G$23),B2+($E$23*B2),IF(AND(B2>$G$23,B2<=$G$24),B2+($E$24*B2),IF(AND(B2>$G$24,B2<=$G$25),B2+($E$25*B2),IF(AND(B2>$G$25,B2<=$G$26),B2+($E$26*B2))))))))))))))))))))))))))
Cells $A2:$A26 & cells $E2:$E26 are variable. Cells $D2:$D26 are just my indicator. Others are constant value.
Cells $B2:$B26 will calculate the formula $A2+$A2* percentage. The percentage is based on the value which is in cells $E2:$E26. The value is then determine by the value in cells $A2:$A26.
Formula in cells $B2:$B26 are as previous formula dragged down.
Example:
If the cost is 50. The value of percentage will be taken from cell $E$7. In this case is 10 thus returning the value in $B$6 to $A6+$A6*10=55
Please refer to ss 2.
excel screenshot 2
If you reach the nesting limit that nearly always means there's an easier way....
You should be able to do this much more simply with a lookup type formula, e.g.
=B2+B2*IF(B2<G$2,E$2,INDEX(E$3:E$26,MATCH(TRUE,INDEX(B2>G$2:G$25,0),0))
This finds the first value in column G which is > B2 and then gets the required value from the next row in column E
The only thing this formula doesn't do is impose an upper limit on B2 (yours is the G26 value 250). If that's an issue you can just add an extra IF to cater for that
You might want to rework your tables but an INDEX/MATCH with a relative lookup on ascending data will return the correct percentage.
=b2*index(e2:e26, match(b2, g2:g26, 1))
Not 100% sure I understand your formula/intent. I think I got it...if not please clarify.
add another column that does the math in column H. enter this formula and drag down.
=($B$2+(E2*$B$2))
in cell B3 enter this formula
=VLOOKUP(ROUNDUP(B2,-1),G:H,2,FALSE)

Exact same CountIf formula giving different results from different cells

I saw an solution where someone was using a range as the criteria for COUNTIF and while trying to understand it better I found some really odd things happening and hoping someone could explain to me what is going on. Here is the setup of the excel.
Name,,Name
Excitebike,,Excitebike
RC Pro Am,,Super Mario Brothers
Punch Out,,Duck Hunt
Super Mario Brothers
Duck Hunt
Hopefully you can use the above to copy and paste it in. In column A there is a list of names and in column C there is a list of some of the names. In cell E1 there is a formula:
=COUNTIF($C$2:C4,$A$2:$A$6)
Then in cell E2 there is the exact same formula.
=COUNTIF($C$2:C4,$A$2:$A$6)
Here is a screen shot so you can see the formulas are identical:
So cell E1 and E2 have the exact same formula but are giving me a different result. As you can see in the first screen shot cell E1 gives a result of 0 while E2 gives a result of 1. Then if I make cell E1 into an array formula it gives a result of 1.
Why would the exact same formula in two different cells give a different result and why when changing cell E1 to an array formula would it change the result? I am using Excel 2016.
UPDATED: Additional questions.
When passing in a array into COUNTIF does it check each element in the range against each element in the criteria or does it just check row in the range against the corresponding row in the criteria?
Even when I put them in the same order, I cannot get the COUNTIF to return a number greater then 1. I would expect if the first 3 match the COUNTIF should return 3 but it is returning 1 for me. Please see below:
While rows 2, 3 and 4 match it is still giving an answer of 1.
I was a little surprised this worked at all. Typically I've used the "criteria" as >4 or <10 etc. Nice to know you can do a string comparison at all.
When using CountIf outside of an array formula you're going to be getting a comparison of values in adjacent cells. Typically CountIF is looking for a single criteria, not a range. At least that's the way I've always used it. eg, first formula in the cell range compared to first cells in each of the cell ranges.
Try these two experiments. Copy Super Mario Brothers from the right column to the left column and the results are going to now show 1 and 1 in the two formulas. Put it back. Move the two cells you have formulas in down one row, and you should see the results go from 1 and 0 to 1 and 1. Move it one more cell lower and it changes the values again.
I'm not sure this is what you're trying to accomplish, but copy this into the formula at and then copy down 6 rows. =COUNTIF($A$2:A$6,C2)

Dynamical SUMIF command

If it is possible, I'd like to create a formula that will allow me the following:
Formula must be in the entire column or in this example, in the range B1:B5. The formula is based on a condition, that when the total sum of cells from column A is lower D1, than it gives "X". If the total sum is over D1, then it gives an empty field - "".
In this example the total sum of the cells, that are over D1 value is in the first 3 rows, hence the three X-es, and then it stops.
(source: shrani.si)
.
I presume it would be possible to do this with multiple SUMIF commands, but does anyone know a smoother solution for this?
Thanks!
You can do this easily by using an absolute reference for the starting point of a SUM and using a relative reference for the end point. When copied down, this formulas works fine.
=IF(SUM($A$1:A1)<$D$1,"x","")
Results
Try this
=IF(SUM(OFFSET(A1,0,0,$A$1:A1,1))>$D$1,"X","")
This formula should start at B1 and then you use auto-increment to populate other cells

EXCEL - Find category by searching keyword from other worksheet

I want to get sales by category (states).
In Sheet1, there're state's names in row A (A1 to A6) and cities in the column.
Each cities belongs to states,
e.g.
Sydney belongs to NSW, and Melbourne & Geelong belong to VIC.
Then Sheet2 contains sales data like shown below.
I want to calculate sales by state each month.
At the moment, each cells between row D and row I has a formula like below;
(e.g. F5)
=IF(AND(ISERR(SEARCH("Brisbane",B5)),ISERR(SEARCH("Gold Coast",B5)),ISERR(SEARCH("Cairns",B5))),"",C5)
But I'm sure there would be better and more simple nifty way to do this. As shops increase, this current formula gets more complicated. So it's needed to be optimized.
I would like to make these calculation simple using vlookup or something.. but no luck so far.
Any advice, detailed if possible, would be greatly appreciated! Thank you in advance :)
You can do this with an array formula. For example, in D3 use:
=IFERROR(IF(INDIRECT("Sheet1!A"&MAX(IF(ISERROR(SEARCH("*"&Sheet1!$B$1:$D$6&"*",$B3))+(Sheet1!$B$1:$D$6=""),0,ROW(Sheet1!$B$1:$D$6))))=D$1,$C3,""),"Not found")
(To enter an array formula, you need to press Ctrl+Shift+Enter)
The SEARCH looks at the store name in B3 and compares it to all names in Sheet1!B1:D6 with a * wildcard before and after. This won't be an error if it matches.
Adding the +(Sheet1!$B$1:$D$6="") gives an OR the cell is blank (otherwise ** would match).
If it isn't an error, we'll get the ROW of the corresponding match and we take the highest (MAX) row match.
We then use INDIRECT to get the cell value in column A of Sheet1 (the State code) and compare that to the state in the top row of our column
This is wrapped in an IFERROR to tell us if our store doesn't match any city
Array formulas are a great tool, but a little confusing! Rather than work on a single cell, array formulas work on each cell of an array. Typically this is for counts, sums, averages, etc. By including “logic maths”, you can do some really strange things in a single formula. A good resource is [link]http://www.cpearson.com/excel/arrayformulas.aspx Always remember to use Ctrl+Shift+Enter or really strange things will happen!

Excel, working out with cells

i have some data on excel and i have on column H a list of solutions and on column G a target box that moves from 0 to 100, and i already have a code to generate the solutions in column H , but i want a code to check the target value on column G and checks column H for the nearest solution number and then puts the answer on column I and highlights it.
thank you very much for your time and effort.
You could do without macro, using a service column, say J, and conditional formatting:
on column I place the formula =MAX(J$1:J$100)-J1 and copy/fill till row 100
similarly fill service column J with formula =ABS(G1-H1)
use conditional formatting in column I to highlight where value=0
You probably will need other function names, because spreadsheets use localized interfaces. For instance, for my test I used LibreOffice in Italian...
HTH
I am assuming that the 'solutions' are positive numbers. If these solutions are arranged in ascending order, then you can use the following formula. If not, one solution can be to use a helper column to sort the values in ascending order.
Let your list of solutions in column H be from H1:H100, and your target box is in cell G1.
Then you can copy the following formula to the cell you want.
=IF(MAX(H1:H100)<=G1,MAX(H1:H100),IF(INDEX(H1:H100,1+MATCH(G1,H1:H100))-G1>G1-INDEX(H1:H100,MATCH(G1,H1:H100)),INDEX(H1:H100,MATCH(G1,H1:H100)),INDEX(H1:H100,1+MATCH(G1,H1:H100))))
Replace H1:H100 everywhere in the above formula, with the range in which solutions are present in ascending order. Also, replace G1 everywhere with the address of the cell with the target value.
If solutions are not in an ascending order, then you can use a helper column. You use one extra column for sorting the solutions into an ascending order. If your solutions are present in H1:H10, then in cell I1, you can enter the following formula.
=IF(ISERROR(SMALL($H$1:$H$100,ROW()-ROW($I$1)+1)),"",SMALL($H$1:$H$100,ROW()-ROW($I$1)+1))
Replace $H$1:$H$100 in the formula with the range in which solutions are present. Also, replace $I$1 with the cell address of the same cell where you are copying this formula. Here, since I am copying this formula into I1, I have used $I$1.
Now copy this formula down till where you have the solutions, e.g. for this example, you will copy it down upto I100 since your solutions are from H1:H100.
Now in the previous formula, replace H1:H100 with I1:I100, as this new list is sorted in an ascending order.
If this is not what you are looking for, maybe you could give me some more details, as your question is not very clear. Hope this helps.
P.S. : You can add the highlighting later, if you get the value that you require.

Resources