Sum up the results inside a FOR loop

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi! I have a code but it doesn't do what It should :D

Code:
For h = 0 To k  'lets suppose from 1 to 4


        capital = 400 * 3 ^ h 
        ossz = 0 + capital
        Next

I would like to sum up 400, 1200, 3600, and 10800, but the "ossz" variable is always the latest value of capital. So at the end of the loop it's 3600, instead of 16.000

Thank you for your help! Have a nice day!
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi! I have a code but it doesn't do what It should :D

Code:
For h = 0 To k  'lets suppose from 1 to 4


        capital = 400 * 3 ^ h 
        [B][COLOR="#FF0000"]ossz = 0 + capital[/COLOR][/B]
        Next

I would like to sum up 400, 1200, 3600, and 10800, but the "ossz" variable is always the latest value of capital. So at the end of the loop it's 3600, instead of 16.000

Try changing the red highlighted line of code to this...

ossz = ossz + capital

so that each new calculated capital value is added to the total of the previous capital values.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top