Loop through row multiplying by one row VBA - excel

I have a set of weights in rows BA40:BZ40 and I want each row starting from BA60:BZ60 to sum (BA40*BA60 + BB40*BB60 + ... + BZ40*BZ60). Then paste this in CA60 then move onto row 61. However I still need to reference BA40:BZ40. I don't know if its my simplistic mind getting confused or its not possible. However I have learnt everything is possible. My code at the moment is
Dim cellsum As Long
For i = 60 to 1000
So I want to calculate for each day, the weighted sum of the loss. For example day 1, sum(0.2*(-10)+ 0.2*(-8)+ 0.1*(-6) + 0.5(-4))
Weight: 20%, 20%, 10%, 50%
Day 1: -10, -8, -6, -5
Day 2: -9, -8, -7, -6
Day 3: -5, -5, -4, -4
...

Use SUMPRODUCT() function as following. It will automatically calculate percentage. You do not need to sum as .2, .1
=SUMPRODUCT(BA40:BZ40,BA60:BZ60)

Imagine the follwing table:
Then the weighted sum of day 1 would calculate according to your example like …
=SUM($B$1*B3,$C$1*C3,$D$1*D3,$E$1*E3)
which is the same as …
=SUMPRODUCT($B$1:$E$1,B3:E3)
This formula in G3 then can easily pulled/copied down until G5.
So for your data it should be something like the following in cell CA60 …
=SUMPRODUCT($BA$40:$BZ$40,BA60:BZ60)
and then copy down to the other rows.

Related

Sum values based on first occurrence of other column using excel formula

Let's say I have the following two columns in excel spreadsheet
A B
1 10
1 10
1 10
2 20
3 5
3 5
and I would like to sum the values from B-column that represents the first occurrence of the value in A-column using a formula. So I expect to get the following result:
result = B1+B4+B5 = 35
i.e., sum column B where any unique value exists in the same row but Column A. In my case if Ai = Aj, then Bi=Bj, where i,j represents the row positions. It means that if two rows from A-column have the same value, then its corresponding values from B-column are the same. I can have the value sorted by column A values, but I prefer to have a formula that works regardless of sorting.
I found this post that refers to the same problem, but the proposed solution I am not able to understand.
Use SUMPRODUCT and COUNTIF:
=SUMPRODUCT(B1:B6/COUNTIF(A1:A6,A1:A6))
Here the step by step explanation:
COUNTIF(A1:A6, A1:A6) will produce an array with the frequency of the values: A1:A6. In our case it will be: {3, 3, 3, 1, 2, 2}
Then we have to do the following division: {10, 10, 10, 20, 5, 5}/{3, 3, 3, 1, 2, 2}. The result will be: {3.33, 3.33, 3.33, 20, 2.5, 2.5}. It replaces each value by the average of its group.
Summing the result we will get: (3.33+3.33+3.33) + 20 + (2.5+2.5=35)=35.
Using the above trick we can just get the same result as if we just sum the first element of each group from the column A.
To make this dynamic, so it grows and shrinks with the data set use this:
=SUMPRODUCT($B$1:INDEX(B:B,MATCH(1E+99,B:B))/COUNTIF($A$1:INDEX(A:A,MATCH(1E+99,B:B)),$A$1:INDEX(A:A,MATCH(1E+99,B:B))))
... or just SUMPRODUCT.
=SUMPRODUCT(B2:B7, --(A2:A7<>A1:A6))

Excel adjustment of values - Max 3 iterations

I have following values in MS Excel.
Years: Y1, Y2, Y3, Y4, Y5, Y6 Y7 Y8 Y9 Y10
Income: -10, 2, 5, 6, -4, 8, -3, 6, 5, 2
Let us assume the Income values are entered in cell range C3:L3
Tax rate is 50% of Net Income. Tax will be Nil if yearly Net income is <= 0. Moreover, if the previous year or years income is negative, it may adjusted against current year's income. The adjustment cannot be done for more than 3 consecutive years. For eg. Loss of Y1(-10) can be adjusted until Y3 only. Y4 will not absorb any previous year's losses. Similarly Y6 will adjust -4 from Y5 and the Net income for Y6 will be 4. The tax in Y6 will be 4*50% = 2
Using the above logic, the corresponding tax amounts(done manually) will be:
Tax: 0, 0, 0, 3, 0, 2, 0, 1.5, 2.5, 1
Can you build an excel formula that can compute the above Tax(shown manually above) and limit the adjustments? No helper rows or columns to be used. I guess Offset() may be used.
Thanks in advance
assuming your formulae are in rows C3 to L3 :
I believe this would work C4:
=50%*MAX(0,C3+MIN(0,MIN(SUM(A3:B3),B3)))
And you can then extend it to the 10 cells.

origin value and after sum after ROUND() is different

I have a table with "ordered amount", "percentages", and "total sum". Note that "ordered amount" and "total sum" should be the same.
column F= sum of C:F
*rows 4,6,8,10 used =round()
As shown in the table(image), for some numbers, value in F is not the same as A when it should be. (e.g. A6(105) and F7(104), -1 after rounding).
Is there anyway to avoid this?
Thank you very much.
If the sum of the decimal points adds up to > 5, your final rounded number will round up. If they add up to < 5, the final number will round down.
Consider this example:
10.5 + 5 = 15.5, which rounds to 16.
However
10 + 5 = 15
This is what is happening in your table.
In order to get the sum of rounded numbers to add up to the same as the sum of the numbers, in your example, you will need to NOT round, but rather subtract, one of the values. I would suggest altering the largest value, as it would seem to have the least effect on the percentages, but that is a choice you can make.
To do that, with your data in row 8, for example you could do the following.
A9: =IF(B$8=MAX($B$8:$E$8),SUM($B$8:$E$8)-SUM(ROUND($B$8:$E$8,0))+ROUND(B$8,0),ROUND(B$8,0))
entered as an array formula with ctrl+shift+enter and fill right to E9.
This would give a sum of 344 which is the same as F8

How to count the number of values that are between intervals in excel

Problem
Let v = {v1, v2, v3,.., vn} be a set of unsorted real numbers.
I want to count to number of values vi that fall in the intervals [0,10], [20,28] etc.
Example
Let v = {2.6, 3.1, 7, 10, 22, 21, 27}
The number of values that fall in the interval [0,10] are: 2.6,3.1,7,10
The number of values that fall in the interval [20, 28] are : 22, 21 ,27
I used the following formula: =SUMPRODUCT((range>0)*(range<10))
Change > and < to >= and <= in your SUMPRODUCT. You can also try COUNTIFS formula - should work faster. With the following sample layout:
Enter in C3 and drag it down:
=COUNTIFS($A$1:$G$1,">=" &A3,$A$1:$G$1,"<="&B3)

Choose a random value between 0.00 and 20.00

In Excel, it is possible to select a random value from a set of 5 options in the following manner:
Values
e.g. 15, 30, 50, 75, or 100
Formula
=CHOOSE(RANDBETWEEN(1,5),15,30,50,75,100)
If I wanted to select a value from a much denser range how would I do it?
e.g. 0.00, 0.01, 0.02, 0.03 ,0.04 ,0.05 ... 19.95, 19.96, 19.97, 19.98, 20.00
What would be the correct formulae?
Consider the following formula:
=0.01*RANDBETWEEN(0,2000)
This will produce random multiples of .01
Scale the output of the random function by multiplying/dividing it by a constant.
For instance, if your Random function outputs floating decimal values between 0 and 1, and you need outputs between 0 and 100, multiply the output of the random function by 100.
If you need the final result to be an integer, you can then round the value to the nearest integer.
It sounds like you want
=ROUND(RAND()*20,2)
The 20 is the maximum value of your zero-to-maximum range, and the 2 is how many decimal places it'll round the final output to.

Resources