How to find the average of the cosecutive specified rows in a column - vba

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.

Related

Excel – return number of unique values in ascending order, optimized for 100,000+ cases

In reference to the excel screen capture below, I’m looking for an optimized formula or VBA solution to do a simple count of the ID values in column A, returning the results in ascending order in column B (this is the desired results). In the example below, there are two '1's in A2 and A3, which would return a '1' and '2' in column B. There are six '2's next in column A, which would return values 1 through 6 in column B, etc.
I have a formula solution that works fine with cases < 20,000 but reaches critical error with datasets >100,000 (I haven’t tried anything between). In B2, the following formula has been used:
=COUNTIF($A$2:$A2,A2)
I’m having trouble finding a solution that doesn’t kick back errors and will complete ~100,000 cases quickly. My current solution runs for hours before kicking back a memory error. Any help with this would be very much appreciated.
Enter in cell B2: 1
Enter in cell B3: =IF(A3=A2,B2+1,1)
Drag formula down
Another way to achieve the desired output is as below...
In B2
=COUNTIF(A$2:A2,A2)
and then copy it down.

subtract the last two non-empty cell in excel

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.

Dynamic calculation in Excel based on a cell value

I've got a challenge in Excel i hope you guys can solve for me.
I have a drop down list (weeks) where i select the week 2015-18 to 2016-17. (nr 1 in picture)
In the cell called LY (nr 2 in picture) I want that to type the result of a dynamic sum range, based on the weeks input.
Picture of setup of the text above
The calculation logic is:
If i select week 2015-20, the VBA or formula should sum(F5:F7)/(G5:G7).
So in other words, I want a dynamic calculation that starts from week 2015-18 (F5/G5) and then sums the values down to the value that i have selected in "Weeks"
Picture of the setup of the logic values
The value of this calculation should be shown in LY (first picture, nr 2).
I really hope you can solve this for me. I've got more rows to calculate, so if you could come up with a "global" code that works for that, it would be great
ok, so you do not need VBA for this.....
for the sake of easier updates; mark the weekrange, right click them and give them a name, with Define name
I called it yearlyweeks
The two drop downs lists, will contain the value corresponding to the text indicating that week.
So we can use this to get a cells address by value (if that text is unique in the range named yearlyweeks):
=ADDRESS(MATCH(H8,yearlyweeks, 0), 2)
where H8 is the cell address of a dropdownlist cell. 2 is the column index of the "B" column
this will result in something like:
$B$2
where the 2 indicates the relative row index, in the range, not the absolute row number.
we are really not interested in the column, or relative row, only the absolute row, but so far we can live with relative..:
=ROW(INDIRECT(ADDRESS(MATCH(H8,yearlyweeks, 0), 2)))
this will give you the relative row of the cell coresponding to what you selected on the drop downlist for one of the lists. So I would do this in two calculation cells, just to avoid the next piece getting too long..
lets let the calculation cells be in I10, and J10..
I10 hold the relative row for the beginning week
J10 for the ending week
below create a new set of cells with:
="F"&(I10 + 7)
and
="G"&(J10 + 7)
where 7 is the row offset of yearlyweeks
in yet one more cell J12 we make the range string:
=CONCATENATE(I11;":";J11)
and then does the calculation in the final cell:
=SUM(INDIRECT(J12))
Now I can do this as a one liner, but you really would hate that

how to count cells in excel with conditional statement?

I want to create a VBA script that can count the number of cells in two columns , for example column A and B, that have values "yes". Moreover, if one of the two cells or both in the same row have value "yes", I want the script to count only 1 time.
For example, if A2 and B2 have value "yes", it is counted 1 time. If A2 or B2 has value "yes", it is also counted 1 time.
Please suggest a solution.
Try this formula.
=SUMPRODUCT((--((A:A="yes")+(B:B="yes"))>0)*1)
I would not advise using whole column references with sumproduct, though, since with over a million rows in Excel 2007 and later, calculation might take a while. You may want to build range names that grow and shrink with the populated rows and use these instead of the column references.
Note that whole column references in SumProduct only work in Excel 2007 and later. For earlier versions of Excel you will need to ringfence a range.

Sum of unfixed number of cells if corresponding cell has given value

I am strugling how to make this happen. I have a worksheet where the data is going to be read in. The size of the data will vary from time to time. I have programmed a generated summation column after the data is read in. I want each cell to sum all values in the row, with the index value in the first row equal to the value in the first row in the summation column. The picture might give you a less abstract visualization of the case. I included the manual formulas for row 3 in row 12 as text. I want to do this in VBA. There might be up to 50 sets ([2010,2011,avvik] or [2010,avvik] or [2011,avvik]). There are two variables with a saved number (column number) for both the start and the end of the data area.
In other words; The money column under "SUM 2010" (which in my program actually is only 2010) should sum every cell in the given row, which has the value 2010 in row 1 in the same column. The same goes for 2011.
(You might want to save/open the picture for details)
You can do this with the formula SUMIF. For Q3, you would write:
=SUMIF(A1:P1, "2010", A3:P:3)
In layman's terms, you are saying, look at all the cells in the range A1 - P1 and for each one, if the value happens to be "2010", I want you to add the value in the range A3 - P3 to the sum.
BTW, you can also use this formula in cell Q11 to get the totals instead of the current formula you have. The fact that the year dates and numbers are in the same column make this really easy.