I want to have a threshold value in one cell(A1) and take it as a reference for adding cells.
Suppose I have
A1 - 10
A2 - 4
A3 - 2
A4 - 3
A5 - 4
A6 - 6
I want to add cells based on A1(Threshold).
As A1 is 10, cells from A6:A5 should be added - Result:10
If A1 is 6 then cell A6 should be returned- Result:6
If A1 is 16 then cells from A6:A3 should be added - Result:19
Is this possible without VBA? Can i get count of number of cells in return along with sum?
I've added some progression SUM operation with ROW and OFFSET for the following. Note that I have modified and added to your sample data for more thorough results.
The SUM formula in C2 is =SUM(OFFSET($A$2,0,0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1,1)) anf the COUNT is derived in D2 with =MAX(INDEX((SUBTOTAL(9,OFFSET($A$2, 0,0,ROW(1:99),1))<$A$1)*ROW(1:99),,))+1. TBH, I didn't experiment much with zeroes in the data as I was unsure whether you would want to count them in the progression or not.
You can use the INDIRECT() function.
=SUM(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
for the count use
=COUNT(INDIRECT("A6:A"&ROUNDDOWN(A1/2,0)))
It's hard to hit a moving target but for your revised parameters try the following.
=SUM(OFFSET($A$6,0-MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,)),0,MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1,1))
=MAX(INDEX((SUBTOTAL(9,OFFSET($A$6,1-ROW(1:5),0,ROW(1:5),1))<$A$1)*ROW(1:5),,))+1
While Excel expects to calculate in a 'down-and-to-the-right' progression, the OFFSET() function will accept parameters to both relocate the starting point and reshape the height and width of the range of cells being summed. Generally, working 'down-and-to-the-right' will allow to leave some breathing room for expansion but you will have to be very careful that you do not attempt to move upwards past row 1 (#REF! error).
OFFSET function
The formula could be simplified if there was any guarantee that nothing of numerical value was ever going to be below A6 but that point has not been addressed so my formulas halt the sum operation at A6.
Related
This is I think a simple problem but I can't seem to find the right solution for it. I don't know if VBA is needed for it. Basically I just want to subtract the last two non-empty cell in Excel. Example: I have the cell A1 and cell B1 and then subtract the value of A1 from B1 and place this value at the same row but another column C1 and so on. I have read this to get the last non-empty cell but I need two non-empty cell and perform operation on them.
=LOOKUP(2,1/(A:A<>""),A:A)
Is there an easier way to do this? Or should I use VBA?
EDIT:
the reason is I'm making a somewhat similar to a balance sheet but a simple one, the user will just enter deposited value at the first column and expenses at the second column and then display the balance in the third column.. and vice versa.
EDIT:
Sample
Put this in C4 and copy down:
=SUM($A$3:$A4)-SUM($B$3:$B4)
If you want to copy the formula past the data so it will automatically fill when data is inserted you can do what #Dirk stated:
=IF(A4&B4<>"",SUM(A$3:A4)-SUM(B$3:B4),"")
Put it in C4 and copy down as far as desired.
This way you can fill the column with the formula and as the data is filled it will change from the empty string to the running total.
EDIT:
Regular formula:
=IF(ROW($C1)=1,OFFSET($C1,0,-2)-OFFSET($C1,0,-1),OFFSET($C1,-1,0)+OFFSET($C1,0,-2)-OFFSET($C1,0,-1))
in cell C3 put "=A3-B3" in cell C4 put "=C3+A4-B4" then copy down.
This will start the balance at 3000 in C3 then add any deposits or minus any withdrawals to the rest of column C.
A very fast way to enter formulas is like this:
Range("E5:H10").Formula = "=A1"
The column and row references adjust, so that for example the formula this code enters in cell H10 is "=D6".
However, that doesn't work for array formulas:
Range("E5:H10").FormulaArray = "=A1"
That puts the array formula {=A1} in each of the cells; no adjustments for rows or columns.
Of course you can loop through all the cells and enter a unique array formula in each one, but that isn't fast.
So I tried this:
Range("E5:H10").Formula = "=A1"
Range("E5:H10").FormulaArray = Range("E5:H10").Formula
But the results were surprising -- the references skip every other row and every other column; for example:
-- The formula in F5 is {=C1} instead of the expected {=B1}, and
-- The formula in D6 is {=A3} instead of the expected {=A2}, and
-- The formula in H10 is {=G11} instead of the expected {=D6}.
WTF?? Why would it skip rows and columns like that?? BTW I'm in Excel 2007 SP3.
For now I'm doing this, which was the fastest way I found that worked:
Range("E5").FormulaArray = "=A1"
Range("E5:H10").FillDown
Range("E5:H10").FillRight
That works but is probably slower than the direct way would be. So -- why does setting the FormulaArray property to the Formula property skip referenced rows and columns as shown above?
The A1 formula is evaluated relative to the top left cell of the range you are populating. So A1 works fine in E5, but B1 is up 4 and left 3 from E5, so the formula entered into F5 refers to the cell up 4 and left 3, which is C1. This repeats for the other cells. The simple solutions is to use:
Range("E5:H10").FormulaArray = Range("E5:H10").FormulaR1C1
since that formula is the same for every cell.
I want to find the average of every 100 rows consecutively in 2 million rows and save these 2000 values in a new column.
Can any one help me either by a VBA or by a Formula in Excel 2007.
waiting for the reply.
The trick here is to set-it up correctly. With the proper set-up, this is a cinch, really. VBA is just going to make this a wee bit easier, but native formulas can do just as well.
Assume I have values in A1:A1000. A1 has 1, A2 has 2, etc. You get the point. Eventually, A1000 has 1000.
Now, copy the following set-up for the specified cells:
B1: 1
C1: 100
B2: =C1+1
C2: =C1+100
Drag down to B10:C10.
Now, input the following formula to D1:
=AVERAGE(INDIRECT("A"&B1&":A"&C1))
Drag down to D10. Voila. You have the average of each 100-row cluster.
Screenshot:
Let us know if this helps.
A single formula:
=INT((ROW()-2)/100)
in say B2 and copied down (to group in hundreds) and a PivotTable with the ColumnB label as Row Labels and Average of the value label in Σ Values is quite straightforward.
If the banding is required, change the formula for =ROW()-1 and Group in the PT.
I am going create a macro to convert two number to a range, e.g.
On excel cell A1 and A2, I input integer 1 and 5 respectively. The output will look like:
A3 1
A4 2
A5 3
A6 4
A7 5
How can I do that?
You can accomplish this without even using macros :)
in A3 type your first number.
On the Home tab go to the Editing section > Fill > Series...
Choose to have your series populated in Columns. Choose a stop value of 20 (or whatever)
Click OK
Now you will have a series of numbers 1-20 going down starting in Cell A3.
You can change your step value so that each number is incremented by 1, or 2 or 3 or whatever. You can also fill in rows instead of columns.
If you need it more automated than that just do all of that while recording a macro and see what it does then change the VBA to suit you more specific needs/come back here with some code to get advice on.
The Below COde will read the Content from the A1 and A2. According to the range it will display the result in the A series only.
enter code here
For i = Cells(1, 1) To Cells(1, 2)
Cells(1, i + 2) = i
Next i
So I have two columns on named program and one with cost values. The three programs are ABC, A, B, and C. I want to sum the costs of all programs that contain A. All that contain B. And all that contain C. ABC clearly is included in all the sums. The problem is that to get just these programs the spreadsheet has a filter on it which messes sumif up. Can someone help? Here is an example of what I mean:
program cost
A 5.00
B 4.00
ABC 9.00
A 2.00
so I would want in three separate cells "sum with A"=16.00, "sum with B"=13.00, "sum with C"=9.00.
Item | Total
A | 16
B | 13
C | 9
Assuming your above range is in A1:B5, my first formula is the following Array formula:
{=SUM(IF(ISERROR(FIND(B6,$A$1:$A$5)),0,$B$1:$B$5))}
You create an Array formula by entering the formula and holding down the Ctrl+Shift keys while you hit Enter. In my solution, I've created an area where I calculate by totals and have a column (called Item in this case) which indicates the letter I see in the original A column.
If you were trying to enter this using VBA, you would use the FormulaArray property:
Selection.FormulaArray ="SUM(IF(ISERROR(FIND(B6,$A$1:$A$5)),0,$B$1:$B$5))"
Update
Restricting the calculation to only visible cells is a bit more complicated. Suppose we have your original data in cells A1:B5. Let's also suppose our test values start in cell C7 (diagonal to the source data). Our totals formula would look like:
=SUMPRODUCT(SUBTOTAL(3,OFFSET($B$1:$B$5,ROW($B$1:$B$5)-ROW($B$1),0,1)), --NOT(ISERROR(FIND(C7,$A$1:$A$5))), $B$1:$B$5)
The following portion returns a range over the cells
OFFSET($B$1:$B$5,ROW($B$1:$B$5)-ROW($B$1),0,1)
This portion returns 1 for each visible cell and 0 for invisible cell
SUBTOTAL(3,OFFSET($B$1:$B$5,ROW($B$1:$B$5)-ROW($B$1),0,1))
This portion is our criteria. NOT(ISERROR(... will return TRUE or FALSE. The double negative sign -- converts that value into a negative integer and then removes that negation.
--NOT(ISERROR(FIND(C7,$A$1:$A$5)))
Lastly, the SUMPRODUCT function multiplies the matching arrays to each other and executes the sum. The first two arrays return a series of 0's or 1's. If the row is both visible and matches our criteria, then we get 1*1 multipled by the given value in the cell. If the given cell is not visible or does not match the criteria, one of the two return a zero and it zeroes out the entire item.