Is it possible to calculate the sum of a whole row? - excel

I have a file that doesn't have a fixed number of columns.
I was wondering if there is a way to tell excel to put the sum of this row in a specific cell, in a way that each time a column is added to the file I don't have to update the formula and increase the range of the sum.
p.s.
I know I can make it a bit easier by just updating the formula like once in 10 times or so. But I was wondering if it's possible to do it once in a lifetime.

Yes sum(1:1) will sum up all values within the first row.
Keep in mind this does not work if the sum needs to be stored in the same row. As you would create a circularity problem.

If your formula will be in b1, and you want to sum from C1 to the end, then simply:
B1: =SUM(C1:XFD1)
Replace XFD with whatever column you think will be far enough to the right that you'll never have to adjust the formula; or leave it as is.
You could make the range dynamic, with something like:
B1: =SUM(OFFSET($A1,0,2,1,LOOKUP(2,1/ISNUMBER(1:1),COLUMN(1:1))-2))
but since that formula is volatile, it may add excessive time to the calculations.

First: Take note of your first cell (Assumed A1)
Second: Take note of your farthest cell (Assumed AZ1)
Then:
=sum(A1:AZ1)

Related

Excel formula to sum as long as

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)

How to transpose Columns to rows (with interruptions) in Excel?

Suppose that we have two columns including a million rows
like this:
What is the right formula or VBA to make another arranged table like this?
For a pure Excel solution try this.
First you need to add a helper column. This just returns TRUE on a header row or FALSE otherwise. This isn't strictly necessary, but it will make the rest of the formulas a little simpler. In the sample you provided above, type this into cell C1:
=IF(A1=B1,TRUE,FALSE)
Now enter this is cell D1:
=IF(AND(C1,NOT(C2)),A2,"")
What this does is check if the current row is a header but not the next, and copies the first cell of the next row if it is, or returns a blank string if it isn't.
Each subsequent cell follows the same pattern, but it first checks if the previous cell is blank. Enter this function into cell E1:
=IF(D1="","",IF(AND(C1,NOT(C3)),A3,""))
Now you just need to copy this pattern outfor another 10 cells. Unfortunately copy-and-paste won't work as you need to increment A3 and C3 downwards, while copy-and-paste with increment them to the right. So, from E1, we get:
F1: =IF(E1="","",IF(AND(C1,NOT(C4)),A4,""))
G1: =IF(F1="","",IF(AND(C1,NOT(C5)),A5,""))
.
.
.
O1: =IF(N1="","",IF(AND(C1,NOT(C13)),A13,""))
Now copy those cells to the bottom of the data set and you should get the results you're after. Here's an example:
Note the extra TRUE in cell C21 so that the last calculation terminates correctly.
However, if you really have a million rows in your data set, I would question the wisdom of using Excel at all. Depending on your circumstances and resources, you may be better off keeping the data in a text file and processing it with a proper scripting language.
Problem Solved in excelforum by JohnTopley here:
http://www.excelforum.com/excel-formulas-and-functions/1174447-interrupted-transpose.html

How to change increment when you drag down the formula?

I have the following formula:
=SUM(INDEX($M50:$N72,COLUMN(B:B),))
When I drag the formula down, it becomes:
=SUM(INDEX($M51:$N73,COLUMN(B:B),))
As you can see the cell increment is by 1.
Question
How can I change increment by other number like, 24, for instance? So that when I drag down the formula should become:
=SUM(INDEX($M74:$N96,COLUMN(B:B),))
This appears what you are asking for (it takes the current row of a formula, multiplies one row less by 24 [assuming you start on row 1 and copy down], and adds in the starting row numbers), although I doubt it does what you want. Are you sure you want the row number of the index function to be "COLUMN(B:B)"?
=SUM(INDEX(INDIRECT("$M"&(ROW()-1)*24+50&":$N"&(ROW()-1)*24+72),COLUMN(B:B)))
It's preferable to avoid the volatile INDIRECT in such situations, if possible. What's more, ROWS is a much better choice than ROW (especially in its unqualified form, i.e. ROW()) here, viz:
=SUM(INDEX($M:$N,50+24*(ROWS($1:1)-1)+1,))
which is equivalent to:
=SUM($M51:$N51)
and, when copied down, to:
=SUM($M75:$N75)
=SUM($M99:$N99)
etc., etc.
I confess I have no idea why you are using a construction such as COLUMN(B:B) for INDEX's row_num parameter.
If you're interested in an explanation as to my statement regarding ROW vs ROWS:
http://excelxor.com/2014/08/25/row-vs-rows-for-consecutive-integer-generation/
Regards

trying to expand vlookup to other cells without automatically changing values

I am setting up a vlookup to pull product prices from another sheet within the workbook. The code works for the cell but when i try to expand or copy and past the code into the next row it automatically changes the data table_array value.
=VLOOKUP(B5,Prices!1:65536,3)
Within the code i want the first value, B5 to scale with the row it is in, however the second value needs to remain the same. How do i go about doing this? Also is there a way that i can get the cell to remain blank instead of displaying N/A if there isnt a valid part number?
Thanks for your help!
=VLOOKUP(B5,Prices!$1:$65536,3)
The $ lock the range.
For example.
$A1 will lock the column to A when the formulas is copied other
locations.
A$1 will lock the row
$A$1 will lock both the column and the row.
I can't comment because I do not have enough rep but this will fix user3716271 's formula:
=IF(ISERROR(VLOOKUP(B5,Prices!$1:$65536,3)),"", VLOOKUP(B5,Prices!$1:$65536,3))
The following formula should solve both problems as well, a little more compact and would use one less VLOOKUP():
=IFERROR(VLOOKUP(B5,Prices!$1:$65536,3), "")
As guitarthrower had said, the $ before the number is used to lock the range.
For the second part, an IF formula will work fine:
=IF(ISERROR(VLOOKUP(B5,Prices!1:65536,3)),"",VLOOKUP(B5,Prices!1:65536,3)),"")
And if I understand correctly the first part have you tried set an absolute value? Something like:
=IF(ISERROR(VLOOKUP(B$5,Prices!1:65536,3)),"",VLOOKUP(B5,Prices!1:65536,3)),"")

Changing cell reference for a SUMIF criterion?

I'm trying to tell Excel to change the cell reference for a SUMIF formula based on the last cell that contains a value in each row. Right now, I know I could write a nested if statement to do this for me, but it's far too unwieldy to be a long-term solution.
Example:
On a separate report, every person in this list has multiple lines recording their daily sales. On a small scale, I'd simply write a SUMIF that uses for A2, B3 and C4 as criteria. The scale is much much larger, so my current solution is to write out a logic check with the SUMIF formula folded into it.
In this case, the formula in E2 would be:
=IF(C2="",if(B2="",SUMIF('range',A2,'range'),sumif('range',B2,'range')),sumif('range',C2,'range'))
Is there a different way to tell Excel to run the SUMIF with the last value found in each row?
I was hoping I could use COUNTA for each row, then have the SUMIF formula use the COUNTA value to change the criterion cell reference. I'm missing something here.
=SUMIF('range',INDEX(A2:C2,MATCH("zzzzz",A2:C2)),'range')
seems worth a try.
Edit Too long for a comment:
I don’t really have any idea how =MATCH() works but think of it as traversing left to right while searching. When it fails to find a match it just happens to have been looking at the last entry and (conveniently!) offers that up. So the key is to look for something that won't be found.
For people’s names I chose “zzzzz” as unlikely to be present. For numbers anything up to 9.99999999999999E+307 is theoretically possible and hence some people use that (eg an hour ago Formula to get the first cell with a numerical value from a selection?) but I prefer a googol (1E+100) and that seems quite large enough, is much shorter – and easier to remember, even if a few 9s more or less would make no difference, nor likely what couple of integers are after the +.

Resources