Averaging daily varying column in excel vb - excel

Every day I have to analyze two cols of numbers.
Cols differ each day.
Col 1 has no.'s from 1 to 5, eg. Day 1 there are 150 x 1's and 200 x 2's, etc. Day 2, 350 x 1's and 85 x 2's etc.
Col 2 has values between 1 and 99.
I need to count how many 1's there are to obtain a 1's average, 2's ave., etc. So far I have tried to write a vb program (excel 2010) - I have written the following:
Function Phil2()
ct = 0
For X = 2 To 10
If ax = 1 Then Let b15 = b15 + bx
ct = ct + 1
Next
End Function.
But I cannot get it to display. Can anyone help me?
I want the average of the 1's in cell b15.

See the formula bar for what is in cell E1. If you don't have XL2007 or above, the formula becomes:
=IF(ISERROR(SUMIF($A$1:$B$10,D1,$B$1:$B$10)/COUNTIF($A$1:$B$10,D1)),"",SUMIF($A$1:$B$10,D1,$B$1:$B$10)/COUNTIF($A$1:$B$10,D1))
You could also make more "automated" by using Dynamic Named Ranges for your ID (1,2,3..) and data (%) sets, that change each day.

OK, It works fine - I modified your formula to:
=IFERROR(AVERAGEIFS(B$16:B$500,$A$16:$A$500,$A2,B$16:B$500,">0"),"")
and it works perfectly for values 1, and 2. So that's a great start. I placed the formula cells on top: so in a1 I typed cow no., in b1 %Milk, in c1 %weight, etc.. In a2 I typed 1, a3 2, a4 3 etc.. In b2 your formula etc.. My next challenge is to lump together all cow types 3 to 11. So next to cow type 1 we have a % for each category, same for cow type 2, etc.. But the 3rd row must have an average for all categories 3+. Raw data cow types are in a10 down, vals in b10, c10, etc.

Related

Calculate results and then count them based on a criteria in one formula

I have the following Excel spreadsheet:
A B C D E
1 Sales 500 200 400 300
2 Difference (Helper Row) -60% 100% -25%
3
4 Criteria: -20%
5 Result: 2
Formulas:
C2 = C1/B1-1
D2 = D1/C1-1
E2 = E1/D1-1
B5 = COUNTIF(B2:E2,"<"&-0.2)
In Row 1 you can see the sales over the last 4 periods. In Row 2 the difference between the sales is calculated using the simple formulas described above.
In Cell B5 I want to show the number of differences (Row 2) which are below the criteria in Cell B4. In this case the result is 2 because -60% and -25% are below the criteria of -20%.
All this works perfectly so far. However, I would prefer to have this system without the Helper Row 2 that I currently use to calculate the differences between the sales in Row 1.
Do you know a formula that gives me the exact same result but calculates the differences automatically and then counts them if they meet a certain criteria?
Try the following
=SUMPRODUCT(--(C1:E1/B1:D1-1 <B4))
If dealing with potential 0s in Sales
={SUM(--(IFERROR(C1:E1/B1:D1-1,0)<B4)*--(C1:E1>0))}

Dragging formulas across - Increment columns by more than 1

I can't seem to find anything similar that's already been asked (they all relate to incrementing row numbers rather than columns)
I'm looking to drag a formula across horizontally and have the columns increment by 2
E.g. B1-A1, D1-C1, F1-E1...
Thanks!
You'll need to have a value in cell A1 and B1 for the following to work.
For my testing I put the number 1 in A1 and B1.
Try this in Cell C1:
=IF(MOD(COUNT($A$1:B1),2)=0,COLUMN(B1),IF(B1<>A1,B1,A1))
Here's what you should see when you drag that formula across:
A B C D E F G H I J K L M N
1 1 2 2 4 4 6 6 8 8 10 10 12 12
And this is what the formula does:
The MOD(COUNT() part of the formula counts the cells to the left of it, and if they are a multiple of 2, the value changes.
I've left the value to change to (the 'new' value) as the COLUMN() number for the cell before, just for example's sake. but you can change this part.
The last IF statement at the end checks if the cell before is equal to the cell before that, (eg. Is CELL C1 equal to CELL B1) and if they are not equal, it will give the cell before as a value (the 'copy' value).

How to get weighted sum depending on multipliers in column in Excel?

I have the table in Excel:
In column C (Sum) I want to get sum this way:
If in column A or B value is 1 then take Amount 48 and multiply by Multiplier (1) = 2.
If in column A or B value is 0 then take Amount 48 and multiply by Multiplier (0) = 1,5.
Then K1 and K2 summed.
So for row 2 the result in column C will be: 48*2 + 48*2 = 192.
For row 5 the result in column C will be: 48*1,5 + 48*2 = 168.
Is it possible to automate this process using Excel formula for C column (inspite of number of columns)?
Or you could use Countif (no shorter though)
=COUNTIF(A2:D2,0)*I$2*I$1+COUNTIF(A2:D2,1)*I$3*I$1
Use Ctrl+Alt+Enter when entering (since it's an array formula)
EDIT: I'm not great with formulas, so there is I'm sure a shorter alernative...

Keep excel offset within worksheet

I have a table like this:
Length 4
year 1 2 3 4 5
A 100 400 300 200 400
B
And in column B I want a sum of A from the past [length] years. For this I figured I needed an OFFSET, so my function is (for year 2):
=SUM.IF(OFFSET(B3;0;0;1;-B1);">0")
The if statement is used so it doesn't give an error when it reaches the edge of the table, but for years 2 and 3 the OFFSET range is outside of the worksheet so it doesn't work. How can I specify a condition that it just doesn't sum anything that isn't on the worksheet?
In A2:
=SUM(INDEX(1:1,COLUMNS($A:A)):INDEX(1:1,MAX(1,COLUMNS($A:A)-3)))
Copy to the right as required.
Regards
Ok, it was hard to decypher the question:
When you ask column B, I guess you mean row 4, right?
You don't need SUMIF, because SUM doesn't count empty cells or cells
with non-numeric value.
The reference to the length value should be absolute, so it doesn't
change as you copy the formula:
$B$1
OFFSET's Width value cannot be negative, rather have the Cols value =
-[Length]:
OFFSET(C3;0; -$B$1...
(Now you are referencing 4 columns left to C3)
Make sure it is not out of the worksheet by not letting more than the [column number of the given cell minus 1] be referenced left from the cell:
OFFSET(C3;0; -MIN(COLUMN(C3)-1;$B$1)...
That is the starting point of your range to sum; you should sum it
up to recent year's value. So the correct formula in C4 is:
=SUM(OFFSET(C3;0;-MIN(COLUMN(C3)-1;$B$1)):C3)

Dividing a column into N equal groups by value

Say I have a column with values:
23
24
25
66
67
84
81
85
I want to divide this into N groups, say N right now is 4.
23,1
24,1
25,2
66,2
67,3
84,3
81,4
85,4
I actually need to divide around 30k sorted values into groups 1 to 99; each with equal number of elements.
Any quick way to do this in Excel?
With data in column A, in B1 enter:
=A1 & "," & ROUNDUP(ROW()/(COUNT(A:A)/4),0)
and copy down. For example:
.
Change the 4 in the formula to vary the number of groups.
I use this trick for equal data bucketing. Suppose you have data in A1:A8 range. Put this formula in B1:
=MAX( ROUNDUP( PERCENTRANK($A$1:$A$8, A1) *4, 0),1)
Fill down the formula all across B column and you are done. The formula divides the range into 4 equal buckets and it returns the bucket number which the cell A1 falls into. The first bucket contains the lowest 25% of values.
Adjust the number of buckets according to thy wish:
=MAX(ROUNDUP(PERCENTRANK([Range],[OneCellOfTheRange]) *[NumberOfBuckets],0),1)
The number of observation in each bucket will be equal or almost equal. For example if you have a 100 observations and you want to split it into 3 buckets then the buckets will contain 33, 33, 34 observations. So almost equal. You do not have to worry about that - the formula works that out for you.
if this is in column A
row 1
row 2
row 3
row 4
row 5
place formula in column B
=MOD(ROW(); 4)+1
this result in
row 1, 2
row 2, 3
row 3, 4
row 4, 1
row 2, 2

Resources